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!