PDA

View Full Version : Running select unit test(s) only



Urthas
31st July 2015, 23:37
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>)

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) {
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;
}

(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!

anda_skoa
1st August 2015, 11:40
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,
_

Urthas
5th August 2015, 17:43
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:


duplicate symbol _main in:
myfirsttest.o
mysecondtest.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [QtTestDriver.app/Contents/MacOS/QtTestDriver] Error 1

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...?

anda_skoa
5th August 2015, 21:18
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,
_