I got my Qt project and I\'m using Qt Creator. I want to unit-test all my code.
However I\'m quite new at QTestLib framework but everyone recommended it for testing Qt-b
I use this approach: http://xilexio.org/?p=125
Namely, place a test config in the single .pro file that builds everything. File hierarchy:
myproject.pro
src/
Example1.cpp
Example2.cpp
Example1.h
Example2.h
test/
ExampleTest.cpp
ExampleTest.h
myproject.pro file:
QT += #needed modules
CONFIG += qt c++11
HEADERS += \
src/Example1.h \
src/Example2.h
SOURCES += \
src/Example1.h \
src/Example2.h
test{
message(Configuring test build...)
TEMPLATE = app
TARGET = myapptests
QT += testlib
HEADERS += \
test/ExampleTest.h
SOURCES += \
test/ExampleTest.cpp
}
else{
TEMPLATE = lib
TARGET = myapp
CONFIG += plugin
TARGET = $$qtLibraryTarget($$TARGET)
}
In my example, I'm building a plugin library, but the method should work for an app as well. In the case of an app, it is likely that SOURCES -= src/main.cpp is needed under the else clause, plugin libraries don't have it. If this is not done, the main() of the app will clash with the main() of the unit tests.
ExampleTest.cpp looks like the following:
#include "ExampleTest.h"
void ExampleTest::exampleTest(){
//Do the tests
}
QTEST_MAIN(ExampleTest)
ExampleTest.h looks like the following:
#include
class ExampleTest : public QObject {
Q_OBJECT
private slots:
void exampleTest();
};
To build the project tests, in a separate directory than the regular build, run:
qmake path/to/myproject.pro "CONFIG += test"