Search code examples
qt-creatorqttest

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


Solution

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