Qt Test Overview

Qt Test is a framework for unit testing Qt based applications and libraries. Qt Test provides all the functionality commonly found in unit testing frameworks as well as extensions for testing graphical user interfaces.

Qt Test is designed to ease the writing of unit tests for Qt based applications and libraries:

Feature

Details

Lightweight

Qt Test consists of about 6000 lines of code and 60 exported symbols.

Self-contained

Qt Test requires only a few symbols from the Qt Core module for non-gui testing.

Rapid testing

Qt Test needs no special test-runners; no special registration for tests.

Data-driven testing

A test can be executed multiple times with different test data.

Basic GUI testing

Qt Test offers functionality for mouse and keyboard simulation.

Benchmarking

Qt Test supports benchmarking and provides several measurement back-ends.

IDE friendly

Qt Test outputs messages that can be interpreted by Qt Creator, Visual Studio, and KDevelop.

Thread-safety

The error reporting is thread safe and atomic.

Type-safety

Extensive use of templates prevent errors introduced by implicit type casting.

Easily extendable

Custom types can easily be added to the test data and test output.

You can use a Qt Creator wizard to create a project that contains Qt tests and build and run them directly from Qt Creator. For more information, see Running Autotests.

Creating a Test

To create a test, subclass QObject and add one or more private slots to it. Each private slot is a test function in your test. QTest::qExec() can be used to execute all test functions in the test object.

In addition, you can define the following private slots that are not treated as test functions. When present, they will be executed by the testing framework and can be used to initialize and clean up either the entire test or the current test function.

  • initTestCase() will be called before the first test function is executed.

  • initTestCase_data() will be called to create a global test data table.

  • cleanupTestCase() will be called after the last test function was executed.

  • init() will be called before each test function is executed.

  • cleanup() will be called after every test function.

Use initTestCase() for preparing the test. Every test should leave the system in a usable state, so it can be run repeatedly. Cleanup operations should be handled in cleanupTestCase(), so they get run even if the test fails.

Use init() for preparing a test function. Every test function should leave the system in a usable state, so it can be run repeatedly. Cleanup operations should be handled in cleanup(), so they get run even if the test function fails and exits early.

Alternatively, you can use RAII (resource acquisition is initialization), with cleanup operations called in destructors, to ensure they happen when the test function returns and the object moves out of scope.

If initTestCase() fails, no test function will be executed. If init() fails, the following test function will not be executed, the test will proceed to the next test function.

Example:

 
Sélectionnez
class MyFirstTest: public QObject
{
    Q_OBJECT

private:
    bool myCondition()
    {
        return true;
    }

private slots:
    void initTestCase()
    {
        qDebug("Called before everything else.");
    }

    void myFirstTest()
    {
        QVERIFY(true); // check that a condition is satisfied
        QCOMPARE(1, 1); // compare two values
    }

    void mySecondTest()
    {
        QVERIFY(myCondition());
        QVERIFY(1 != 2);
    }