Qt Test Best Practices

We recommend that you add Qt tests for bug fixes and new features. Before you try to fix a bug, add a regression test (ideally automatic) that fails before the fix, exhibiting the bug, and passes after the fix. While you're developing new features, add tests to verify that they work as intended.

Conforming to a set of coding standards will make it more likely for Qt autotests to work reliably in all environments. For example, some tests need to read data from disk. If no standards are set for how this is done, some tests won't be portable. For example, a test that assumes its test-data files are in the current working directory only works for an in-source build. In a shadow build (outside the source directory), the test will fail to find its data.

The following sections contain guidelines for writing Qt tests:

General Principles

The following sections provide general guidelines for writing unit tests:

Verify Tests

Write and commit your tests along with your fix or new feature on a new branch. Once you're done, you can check out the branch on which your work is based, and then check out into this branch the test-files for your new tests. This enables you to verify that the tests do fail on the prior branch, and therefore actually do catch a bug or test a new feature.

For example, the workflow to fix a bug in the QDateTime class could be like this if you use the Git version control system:

  1. Create a branch for your fix and test: git checkout -b fix-branch 5.14

  2. Write a test and fix the bug.

  3. Build and test with both the fix and the new test, to verify that the new test passes with the fix.

  4. Add the fix and test to your branch: git add tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp src/corelib/time/qdatetime.cpp

  5. Commit the fix and test to your branch: git commit -m 'Fix bug in QDateTime'

  6. To verify that the test actually catches something for which you needed the fix, checkout the branch you based your own branch on: git checkout 5.14

  7. Checkout only the test file to the 5.14 branch: git checkout fix-branch -- tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp

    Only the test is now on the fix-branch. The rest of the source tree is still on 5.14.

  8. Build and run the test to verify that it fails on 5.14, and therefore does indeed catch a bug.

  9. You can now return to the fix branch: git checkout fix-branch

  10. Alternatively, you can restore your work tree to a clean state on 5.14: git checkout HEAD -- tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp

When you're reviewing a change, you can adapt this workflow to check that the change does indeed come with a test for a problem it does fix.

Give Test Functions Descriptive Names

Naming test cases is important. The test name appears in the failure report for a test run. For data-driven tests, the name of the data row also appears in the failure report. The names give those reading the report a first indication of what has gone wrong.

Test function names should make it obvious what the function is trying to test. Do not simply use the bug-tracking identifier, because the identifiers become obsolete if the bug-tracker is replaced. Also, some bug-trackers may not be accessible to all users. When the bug report may be of interest to later readers of the test code, you can mention it in a comment alongside a relevant part of the test.

Likewise, when writing data-driven tests, give descriptive names to the test-cases, that indicate what aspect of the functionality each focuses on. Do not simply number the test-case, or use bug-tracking identifiers. Someone reading the test output will have no idea what the numbers or identifiers mean. You can add a comment on the test-row that mentions the bug-tracking identifier, when relevant.

Write Self-contained Test Functions

Within a test program, test functions should be independent of each other and they should not rely upon previous test functions having been run. You can check this by running the test function on its own with tst_foo testname.

Do not re-use instances of the class under test in several tests. Test instances (for example widgets) should not be member variables of the tests, but preferably be instantiated on the stack to ensure proper cleanup even if a test fails, so that tests do not interfere with each other.

Test the Full Stack

If an API is implemented in terms of pluggable or platform-specific backends that do the heavy-lifting, make sure to write tests that cover the code-paths all the way down into the backends. Testing the upper layer API parts using a mock backend is a nice way to isolate errors in the API layer from the backends, but it is complementary to tests that run the actual implementation with real-world data.

Make Tests Complete Quickly

Tests should not waste time by being unnecessarily repetitious, by using inappropriately large volumes of test data, or by introducing needless idle time.

This is particularly true for unit testing, where every second of extra unit test execution time makes CI testing of a branch across multiple targets take longer. Remember that unit testing is separate from load and reliability testing, where larger volumes of test data and longer test runs are expected.

Benchmark tests, which typically execute the same test multiple times, should be located in a separate tests/benchmarks directory and they should not be mixed with functional unit tests.

Use Data-driven Testing

Data-driven tests make it easier to add new tests for boundary conditions found in later bug reports.

Using a data-driven test rather than testing several items in sequence in a test saves repetition of very similar code and ensures later cases are tested even when earlier ones fail. It also encourages systematic and uniform testing, because the same tests are applied to each data sample.

Use Coverage Tools

Use a coverage tool such as Froglogic Coco Code Coverage or gcov to help write tests that cover as many statements, branches, and conditions as possible in the function or class being tested. The earlier this is done in the development cycle for a new feature, the easier it will be to catch regressions later when the code is refactored.

Select Appropriate Mechanisms to Exclude Tests

It is important to select the appropriate mechanism to exclude inapplicable tests: QSKIP(), using conditional statements to exclude parts of a test function, or not building the test for a particular platform.

Use QSKIP() to handle cases where a whole test function is found at run-time to be inapplicable in the current test environment. When just a part of a test function is to be skipped, a conditional statement can be used, optionally with a qDebug() call to report the reason for skipping the inapplicable part.

Test functions or data rows of a data-driven test can be limited to particular platforms, or to particular features being enabled using #if. However, beware of moc limitations when using #if to skip test functions. The moc preprocessor does not have access to all the builtin macros of the compiler that are often used for feature detection of the compiler. Therefore, moc might get a different result for a preprocessor condition from that seen by the rest of your code. This may result in moc generating meta-data for a test slot that the actual compiler skips, or omitting the meta-data for a test slot that is actually compiled into the class. In the first case, the test will attempt to run a slot that is not implemented. In the second case, the test will not attempt to run a test slot even though it should.

If an entire test program is inapplicable for a specific platform or unless a particular feature is enabled, the best approach is to use the parent directory's .pro file to avoid building the test. For example, if the tests/auto/gui/someclass test is not valid for macOS, add the following line to tests/auto/gui.pro:

 
Sélectionnez
mac*: SUBDIRS -= someclass

Avoid Q_ASSERT

The Q_ASSERT macro causes a program to abort whenever the asserted condition is false, but only if the software was built in debug mode. In both release and debug-and-release builds, Q_ASSERT does nothing.

Q_ASSERT should be avoided because it makes tests behave differently depending on whether a debug build is being tested, and because it causes a test to abort immediately, skipping all remaining test functions and returning incomplete or malformed test results.

It also skips any tear-down or tidy-up that was supposed to happen at the end of the test, and might therefore leave the workspace in an untidy state, which might cause complications for further tests.

Instead of Q_ASSERT, the QCOMPARE() or QVERIFY() macro variants should be used. They cause the current test to report a failure and terminate, but allow the remaining test functions to be executed and the entire test program to terminate normally. QVERIFY2() even allows a descriptive error message to be recorded in the test log.

Writing Reliable Tests

The following sections provide guidelines for writing reliable tests:

Avoid Side-effects in Verification Steps

When performing verification steps in an autotest using QCOMPARE(), QVERIFY(), and so on, side-effects should be avoided. Side-effects in verification steps can make a test difficult to understand. Also, they can easily break a test in ways that are difficult to diagnose when the test is changed to use QTRY_VERIFY(), QTRY_COMPARE() or QBENCHMARK(). These can execute the passed expression multiple times, thus repeating any side-effects.

When side-effects are unavoidable, ensure that the prior state is restored at the end of the test function, even if the test fails. This commonly requires use of an RAII (resource acquisition is initialization) class that restores state when the function returns, or a cleanup() method. Do not simply put the restoration code at the end of the test. If part of the test fails, such code will be skipped and the prior state will not be restored.

Avoid Fixed Timeouts

Avoid using hard-coded timeouts, such as QTest::qWait() to wait for some conditions to become true. Consider using the QSignalSpy class, the QTRY_VERIFY() or QTRY_COMPARE() macros, or the QSignalSpy class in conjunction with the QTRY_ macro variants.

The qWait() function can be used to set a delay for a fixed period between performing some action and waiting for some asynchronous behavior triggered by that action to be completed. For example, changing the state of a widget and then waiting for the widget to be repainted. However, such timeouts often cause failures when a test written on a workstation is executed on a device, where the expected behavior might take longer to complete. Increasing the fixed timeout to a value several times larger than needed on the slowest test platform is not a good solution, because it slows down the test run on all platforms, particularly for table-driven tests.

If the code under test issues Qt signals on completion of the asynchronous behavior, a better approach is to use the QSignalSpy class to notify the test function that the verification step can now be performed.

If there are no Qt signals, use the QTRY_COMPARE() and QTRY_VERIFY() macros, which periodically test a specified condition until it becomes true or some maximum timeout is reached. These macros prevent the test from taking longer than necessary, while avoiding breakages when tests are written on workstations and later executed on embedded platforms.

If there are no Qt signals, and you are writing the test as part of developing a new API, consider whether the API could benefit from the addition of a signal that reports the completion of the asynchronous behavior.

Beware of Timing-dependent Behavior

Some test strategies are vulnerable to timing-dependent behavior of certain classes, which can lead to tests that fail only on certain platforms or that do not return consistent results.

One example of this is text-entry widgets, which often have a blinking cursor that can make comparisons of captured bitmaps succeed or fail depending on the state of the cursor when the bitmap is captured. This, in turn, may depend on the speed of the machine executing the test.

When testing classes that change their state based on timer events, the timer-based behavior needs to be taken into account when performing verification steps. Due to the variety of timing-dependent behavior, there is no single generic solution to this testing problem.

For text-entry widgets, potential solutions include disabling the cursor blinking behavior (if the API provides that feature), waiting for the cursor to be in a known state before capturing a bitmap (for example, by subscribing to an appropriate signal if the API provides one), or excluding the area containing the cursor from the bitmap comparison.

Avoid Bitmap Capture and Comparison

While verifying test results by capturing and comparing bitmaps is sometimes necessary, it can b