Reputation: 1684
I have a fairly large Qt GUI project, which unfortunately lacks proper unit tests.
I'm planning to have a test project with a test source file for every class.
I digged through a lot of Qt's official docs, however there are some questions left:
Any pointers to tutorials covering hint testing of a larger Qt project are highly appreciated.
Upvotes: 3
Views: 3017
Reputation:
I did not have a lot of time to do perfect unit testing in Qt. I did, however, figure out how to get the main plumbing working. I hope that this can help you, although you could probably do a better job than I did by studying the Writing Unit Tests guide.
How can I create a test suite? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently?
You can organize "suites" by collecting unit tests (which are slots on objects) into objects and composing those objects.
test_example.pro
TARGET = test_example
TEMPLATE = app
CONFIG += testcase
QT += testlib core
... INCLUDEPATH, SOURCES, HEADERS, etc...
main.cpp
#include <QtTest/QtTest>
#include "MainTest.h"
// This macro introduces an event loop
QTEST_MAIN(MainTest)
MainTest.h
class MainTest : public QObject
{
Q_OBJECT
private slots:
// Each slot is a unit test
void init()
{
}
// Create a "test suite" by bundling tests into objects
void runFirstSuite()
{
FirstSuiteUiTest first;
first.test(); // runs 10 tests
FirstSuiteIoTest second;
second.test(); // runs 999 tests
...
}
void SecondSuite()
{
QFAIL("This suite is not really a suite, and it always fails");
}
};
You can also organize tests into physical file structures. I used a subdirs project as my root .pro file so I could build artifacts (dynamic and static archives, binaries) and run many collections of unit tests.
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += src/core
SUBDIRS += src/some_dll
SUBDIRS += src/test/awesome_core_test_1 #Holds many suites
SUBDIRS += src/test/awesome_core_test_2 #Holds many other suites!
core.depends = core
awesome_core_test_1.depends = core
awesome_core_test_2.depends = core
So by organizing unit tests into object compositions and physical files, you can some granularity of organization. There may be better or different ways, but I am not sure.
How can I integrate the test execution into Qt Creator? Maybe running the tests as post-build step would be interesting?
Add CONFIG += testcase
into your .pro file. This creates a build target. Invoke the build and tests by running make check
Should I add all normal source files to the test project or is there a way to create a static lib?
You can do either, just like in any other project.
Upvotes: 3
Reputation: 1782
How can I create a test suite? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently?
In "Qt unit test class" each private slot is a unit test. Not exists other standard ways for adding new tests, but exist non documentary qmake feature. You can read about in this forum thread
How can I integrate the test execution into Qt Creator? Maybe running the tests as post-build step would be interesting?
You can add CONFIG+=testcase test project and this will generate additional make target - check. Of course, you can add this build step anywhere. Also, you can read about here - Writing_Unit_Tests
Should I add all normal source files to the test project or is there a way to create a static lib?
You can create static or dynamic lib with sources and link them to test project, or you can add sources to test project like here
By the way, you can watch example of unit testing with Qt here
Upvotes: 0