How to run qtestlib unit tests from QtCreator

I am developing a GUI application in Qt Creator and want to write some unit tests for it.

I followed this guide to do some unit tests with QtTestlib, and the program compiles in order. But how do I run them? I would like them to start before the GUI application starts, if the buid debugs and won't start if the assembly is released.

+5
source share
4 answers

Finally, it turned out how to run tests before running the application.

I added one static method in the test class to run the tests:

#include <QtTest/QtTest>

TestClass::runTests()
{
    TestClass * test = new TestClass();

    QTest::qExec(test);
    delete test;
}

In the main function, do:

int main(int argv, char *args[])
{
    ::TestsClas::runTests();

    QApplication app(argv, args);
    MainWindow mainWindow;
    mainWindow.setGeometry(100, 100, 800, 500);
    mainWindow.show();

    return app.exec();
}

.

+4

. , . .

, unit test . script .

. ( , , ), , . FrogLogic Squish - .

+18

:

int main(int argv, char *args[])
{
#ifdef TEST
    ::TestsClas::runTests();
#endif
    QApplication app(argv, args);
    MainWindow mainWindow;
    mainWindow.setGeometry(100, 100, 800, 500);
    mainWindow.show();

    return app.exec();
}

"", "". " " " "

CXXFLAGS+=-DTEST

, , .

+5
source

The creator of Qt does not yet explicitly support the current unit tests (up to Qt Creator 2.0beta). So for now, you will need to manually start the tests.

If you use a build system such as cmake instead of qmake, you can try to automatically run unit tests as part of the build process itself. Unfortunately, I don't know which way to do this with qmake. CMake is supported by the creator of Qt, although not in the same way as qmake.

+2
source

All Articles