Results 1 to 7 of 7

Thread: QtTest - use it in your application

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtTest - use it in your application

    The QTEST_MAIN macro creates a main function so you can only have one per application. The standard approach is to have a different executable for each unit test. This is easy to do with CMake, but more annoying with qmake. An alternative is to write your own main function that does multiple unit tests.

    The code below could prove useful if you take the second path :
    Qt Code:
    1. // header
    2.  
    3. class Test {
    4. public:
    5. static Test* get();
    6.  
    7.  
    8. int runAll(int argc, char **argv);
    9. void addTestObject(QObject *o);
    10.  
    11.  
    12. private:
    13. Test();
    14. ~Test();
    15.  
    16.  
    17. QList<QObject*> m_testObjects;
    18. };
    19.  
    20.  
    21. template <class T>
    22. class TestRegistar {
    23. public:
    24. inline TestRegistar(const char *name) {
    25. T *test = new T;
    26. test->setObjectName(name);
    27. Test::get()->addTestObject(test);
    28. }
    29. };
    30.  
    31. #define TEST(T) \
    32. static ::TestRegistar<T> _test_registar_##T(""#T);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //source
    2.  
    3.  
    4. Test* Test::get() {
    5. static Test inst;
    6. return &inst;
    7. }
    8.  
    9.  
    10. /*!
    11.   \brief Run all unit tests
    12.  
    13.  
    14.   The following arguments are recognized :
    15.   <ul>
    16.   <li>--restrict : only run tests to which an option is passed
    17.   <li>@&lt;testname&gt;[:&lt;option&gt;] : pass an argument to a single test.
    18.   The test will be run with --restrict on even if the option is empty
    19.   </ul>
    20.   All other arguments are forwarded to all unit tests. Arguments passed to test
    21.   can be any of those supported by regular QtTest-based unit tests.
    22. */
    23. int Test::runAll(int argc, char **argv) {
    24. bool restrict = false;
    25. QStringList common;
    26. QHash<QString, QStringList> specific;
    27. for (int i = 0; i < argc; ++i) {
    28. QString s = QString::fromLocal8Bit(argv[i]);
    29. if (i && *argv[i] == '@') {
    30. int idx = s.indexOf(':');
    31. if (idx != -1)
    32. specific[s.mid(1, idx - 1)] << s.mid(idx + 1);
    33. else
    34. specific[s.mid(1)] = specific.value(s.mid(1));
    35. } else if (!qstrcmp(argv[i], "--restrict")) {
    36. restrict = true;
    37. } else {
    38. common << s;
    39. }
    40. }
    41.  
    42.  
    43. int ret = 0;
    44. foreach (QObject *o, m_testObjects) {
    45. if (restrict && !specific.contains(o->objectName()))
    46. continue;
    47. QStringList args = common;
    48. args << specific.value(o->objectName());
    49. if ((ret = QTest::qExec(o, args)))
    50. break;
    51. }
    52. return ret;
    53. }
    54.  
    55.  
    56. void Test::addTestObject(QObject *o) {
    57. m_testObjects << o;
    58. }
    59.  
    60.  
    61. Test::Test() {
    62. }
    63.  
    64.  
    65.  
    66. Test::~Test() {
    67. qDeleteAll(m_testObjects);
    68. }
    To copy to clipboard, switch view to plain text mode 

    With that, you can register multiple unit test classes with the TEST macro and invoke them all from anywhere you fancy (but most likely from a main()) via "Test::get()->runAll(argc, argv)"
    Current Qt projects : QCodeEdit, RotiDeCode

  2. The following user says thank you to fullmetalcoder for this useful post:

    migel (23rd December 2011)

Similar Threads

  1. qttest output
    By BalaQT in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2011, 17:17
  2. QtTest and asserts
    By doberkofler in forum Qt Programming
    Replies: 11
    Last Post: 21st September 2010, 07:22
  3. QTTest and my own gui
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 19th March 2010, 10:26
  4. QtTest example not compiling
    By GrahamLabdon in forum Newbie
    Replies: 3
    Last Post: 19th March 2010, 09:41
  5. QtTest bug
    By graeme in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.