Results 1 to 4 of 4

Thread: Running select unit test(s) only

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Running select unit test(s) only

    Hello,

    I've just started using the QtTest module. The tutorial shows a method for creating a stand-alone executable test case, which is very nice. What I would like to do is make each test case stand-alone executable so that I can run this or that one, or write a script that runs combinations, etc. But it seems that the only way to do that is for each test case to live in its own folder. This is because

    Qt Code:
    1. QTEST_MAIN(<classname>)
    To copy to clipboard, switch view to plain text mode 

    at the bottom of the .cpp files expands into a main() function, and like Highlander, there can be only one.

    What I have ended up doing is throwing the QTEST_MAIN approach to the wind and creating a sub-project in the code under test. This sub-project contains the .pro file, test cases, and a main.cpp which looks like this:

    Qt Code:
    1. #include "testoutgoingcall.h"
    2. #include "testscopelock.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. if (argc == 1) { // no user args, so test all
    7. QList<TestCase *> testCases;
    8. testCases << new TestOutgoingCall() <<
    9. new TestScopeLock();
    10. foreach (TestCase *testCase, testCases)
    11. QTest::qExec(testCase);
    12. qDeleteAll(testCases);
    13. testCases.clear();
    14. }
    15. else {
    16. TestCase *testCase = 0;
    17. for (int i = 1; i < argc; ++i) {
    18. QString className = argv[i];
    19. className = className.toLower();
    20. if (className == "testoutgoingcall")
    21. testCase = new TestOutgoingCall();
    22. else if (className == "testscopelock")
    23. testCase = new TestScopeLock();
    24. QTest::qExec(testCase);
    25. delete testCase;
    26. }
    27. }
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

    (As you can see, I created a trivial TestCase base class to make the code a little less repetitive.) Pretty straightforward, and works like a charm.

    My question is: is there a better way to run test cases alone or in combination? If so, how should I go about it? What was I missing in the tutorial?

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Running select unit test(s) only

    Quote Originally Posted by Urthas View Post
    at the bottom of the .cpp files expands into a main() function, and like Highlander, there can be only one.
    There can only be one main() function per executable, but you can have (a) any number of executables in the same directory and (b) any number of test functions in one executable.

    Usually one would have one test executable for one testable unit, e.g. one class, and each of those test executables would have all the tests for that specific unit.

    That allows to run all tests (running all executables), only those of specific units (running only those executables) or running specific tests of specific units (by running those executables and specifying the test function names as their arguments).

    Cheers,
    _

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Running select unit test(s) only

    Quote Originally Posted by anda_skoa View Post
    you can have (a) any number of executables in the same directory
    Thank you for your reply. Hmm. With two test cases in the same directory, each stand-alone executable via the QTEST_MAIN macro and moc #include, running the following commands per the tutorial:

    /myTestDirectory$ qmake -project "QT += testlib"
    /myTestDirectory$ qmake
    /myTestDirectory$ make

    gives the following error:

    Qt Code:
    1. duplicate symbol _main in:
    2. myfirsttest.o
    3. mysecondtest.o
    4. ld: 1 duplicate symbol for architecture x86_64
    5. clang: error: linker command failed with exit code 1 (use -v to see invocation)
    6. make: *** [QtTestDriver.app/Contents/MacOS/QtTestDriver] Error 1
    To copy to clipboard, switch view to plain text mode 

    Clearly I do not understand the overall process very well. Can you please point me in the right direction? For example, should I be investigating how qmake is invoked, or...?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Running select unit test(s) only

    qmake -project tries to create a QMake project file from the contents of a directory by using some heuristic.

    You just don't do that.
    Create one .pro file per executable and one .pro file with template type subdirs that lists all the test .pro files in its SUBDIRS variable.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Urthas (5th August 2015)

Similar Threads

  1. Unit Test Tool for QT
    By mythili in forum Installation and Deployment
    Replies: 5
    Last Post: 1st May 2017, 18:07
  2. Unit Test: Test if QPushButton has a menu
    By FelixB in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2012, 12:12
  3. debugging a unit test
    By Martin Drozdik in forum Newbie
    Replies: 4
    Last Post: 1st June 2012, 01:38
  4. Problem with Qt Unit Test
    By rubikon in forum Qt Programming
    Replies: 3
    Last Post: 25th November 2011, 09:18
  5. How to unit test a Qt Gui
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2006, 07:36

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.