views:

274

answers:

3

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

I followed This guide to make some unit tests with QtTestlib and the program compiles fine. But how do I run them? I would like them to be run before the GUI app starts if debug buid and not run if release build.

+1  A: 

Qt creator does not yet explicitly support running unit tests at this time (up to Qt Creator 2.0beta). So for the time being you will need to start the tests manually.

If you are using a build system like cmake instead of qmake then you could try to run the unit tests automatically as part of the build process itself. Unfortunately I am not aware of any method to do this with qmake. CMake is supported by Qt creator, although not as well as qmake.

Tobias Hunger
+2  A: 

Finally figured out how to run tests before starting the app.

I added one static method in the tests 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();
}

The test results are printed in application output window.

extropy
A: 

No, this is not the way to do it!

You should create a project for your unit tests, build and run that. You should NOT modify your main project to run the tests. Also, you should have a build server set up that will automatically invoke your unit tests and build your releases, you can script this.

But please do not hack your main application to run your unit tests!

James Oltmans