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
QTEST_MAIN(<classname>)
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:
#include "testoutgoingcall.h"
#include "testscopelock.h"
int main(int argc, char *argv[])
{
if (argc == 1) { // no user args, so test all
QList<TestCase *> testCases;
testCases << new TestOutgoingCall() <<
new TestScopeLock();
foreach (TestCase *testCase, testCases)
QTest::qExec(testCase);
qDeleteAll(testCases);
testCases.clear();
}
else {
TestCase *testCase = 0;
for (int i = 1; i < argc; ++i) {
className = className.toLower();
if (className == "testoutgoingcall")
testCase = new TestOutgoingCall();
else if (className == "testscopelock")
testCase = new TestScopeLock();
QTest::qExec(testCase);
delete testCase;
}
}
return 0;
}
#include "testoutgoingcall.h"
#include "testscopelock.h"
int main(int argc, char *argv[])
{
if (argc == 1) { // no user args, so test all
QList<TestCase *> testCases;
testCases << new TestOutgoingCall() <<
new TestScopeLock();
foreach (TestCase *testCase, testCases)
QTest::qExec(testCase);
qDeleteAll(testCases);
testCases.clear();
}
else {
TestCase *testCase = 0;
for (int i = 1; i < argc; ++i) {
QString className = argv[i];
className = className.toLower();
if (className == "testoutgoingcall")
testCase = new TestOutgoingCall();
else if (className == "testscopelock")
testCase = new TestScopeLock();
QTest::qExec(testCase);
delete testCase;
}
}
return 0;
}
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!
Bookmarks