How to run qtestlib unit tests from QtCreator
Asked Answered
S

4

5

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.

Sensibility answered 11/5, 2010 at 8:18 Comment(0)
S
3

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.

Sensibility answered 31/5, 2010 at 12:20 Comment(1)
You don't want to pollute your application main with test code.Misprize
M
17

Do not put test code into your main project. You should create a separate project for your unit tests then build and run that. Do not modify your main project to run tests.

Ideally, you should have a build server set up that will automatically invoke your unit test project and build your releases. You can script this.

Never hack your main application to run your unit tests. If you need to do integration level testing (i.e. testing how the program works once it is fully compiled and integrated) you should use a different integration testing framework that allows you to test the program from an externally scripted source. FrogLogic's Squish is one such framework.

Moneymaking answered 8/10, 2010 at 21:33 Comment(2)
Hi James. Let's say I have an application with a bunch of menulist items. Each menu item when clicked shows a form. How do I make a unit test that clicks on every item of the menu list and checks if the right form is shown?Kironde
You can't make a unit test for that. You need something like FrogLogic's Squish to do that kind of integrated testing. Unit testing is much lower level than UI testing. At least that's the way I understand it working. If you find that you can do this kind of stuff I would love to know but everything I read said that this is a misuse of unit testing.Moneymaking
A
5

Use multiple targets and preprocessor flags to achieve this:

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();
}

Then go into the projects pane and add a new target "Test" by duplicating "Debug". Under Build Steps, add an argument to Make that is

CXXFLAGS+=-DTEST

That way the test is included in the Test target, but not the Debug or Release targets.

Alidia answered 27/5, 2011 at 11:41 Comment(1)
This cannot view and run tests in Qt Creator IDE.Treatment
S
3

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.

Sensibility answered 31/5, 2010 at 12:20 Comment(1)
You don't want to pollute your application main with test code.Misprize
A
2

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.

Abiding answered 31/5, 2010 at 7:19 Comment(1)
Personally, I find cmake support to be quite good. I even use CTEST to batch run my QTEST unit testing classes through Creator. in a custom build configuration.Misprize

© 2022 - 2024 — McMap. All rights reserved.