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 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. |
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, there are four private slots that are not treated as test functions. 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.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.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:
class MyFirstTest: public QObject { Q_OBJECT private slots: void initTestCase() { qDebug("called before everything else"); } void myFirstTest() { QVERIFY(1 == 1); } void mySecondTest() { QVERIFY(1 != 2); } void cleanupTestCase() { qDebug("called after myFirstTest and mySecondTest"); } };
For more examples, refer to the Qt Test Tutorial.
If you are using qmake
as your build tool, just add the following to your project file:
QT += testlib
If you would like to run the test via make check
, add the additional line:
CONFIG += testcase
See the qmake manual for more information about make check
.
If you are using other build tools, make sure that you add the location of the Qt Test header files to your include path (usually include/QtTest
under your Qt installation directory). If you are using a release
build of Qt, link your test to the QtTest
library. For debug builds, use QtTest_debug
.
See Writing a Unit Test for a step by step explanation.
The syntax to execute an autotest takes the following simple form:
testname [options] [testfunctions[:testdata]]...
Substitute testname
with the name of your executable. testfunctions
can contain names of test functions to be executed. If no testfunctions
are passed, all tests are run. If you append
the name of an entry in testdata
, the test function will be run only with that test data.
For example:
/myTestDirectory$ testQString toUpper
Runs the test function called toUpper
with all available test data.
/myTestDirectory$ testQString toUpper toInt:zero
Runs the toUpper
test function with all available test data, and the toInt
test function with the test data called zero
(if the specified test data doesn't exist, the associated test
will fail).
/myTestDirectory$ testMyWidget -vs -eventdelay 500
Runs the testMyWidget
function test, outputs every signal emission and waits 500 milliseconds after each simulated mouse/keyboard event.
The following command line options determine how test results are reported:
-o
filename,formattxt
, xml
, lightxml
or xunitxml
). The special filename -
may be used to log to standard
output.-o
filename-txt
-xml
-lightxml
-xunitxml
-csv
-teamcity
The first version of the -o
option may be repeated in order to log test results in multiple formats, but no more than one instance of this option can log test results to standard output.
If the first version of the -o
option is used, neither the second version of the -o
option nor the -txt
, -xml
, -lightxml
, -teamcity
or
-xunitxml
options should be used.
If neither version of the -o
option is used, test results will be logged to standard output. If no format option is used, test results will be logged in plain text.
The following command line options control how much detail is reported in test logs:
-silent
-v1
-v2
-v1
for plain text
output.)
-vs
The following command-line options influence how tests are run:
-functions
-datatags
-eventdelay
ms-keydelay
ms-mousedelay
ms-maxwarnings
number-nocrashhandler
-platform
nameThe following command line options control benchmark testing:
-callgrind
-tickcounter
-eventcounter
-minimumvalue
n-minimumtotal
n-iterations
n-median
n-vb
-help
To create a benchmark, follow the instructions for creating a test and then add a QBENCHMARK macro to the test function that you want to benchmark.
class MyFirstBenchmark: public QObject { Q_OBJECT private slots: void myFirstBenchmark() { QString string1; QString string2; QBENCHMARK { string1.localeAwareCompare(string2); } } };
The code inside the QBENCHMARK macro will be measured, and possibly also repeated several times in order to get an accurate measurement. This depends on the selected measurement back-end. Several back-ends are available. They can be selected on the command line:
Name | Command-line Argument | Availability |
---|---|---|
Walltime | (default) | All platforms |
CPU tick counter | -tickcounter | Windows, macOS, Linux, many UNIX-like systems. |
Event Counter | -eventcounter | All platforms |
Valgrind Callgrind | -callgrind | Linux (if installed) |
Linux Perf | -perf | Linux |
In short, walltime is always available but requires many repetitions to get a useful result. Tick counters are usually available and can provide results with fewer repetitions, but can be susceptible to CPU frequency scaling issues. Valgrind provides exact results, but does not take I/O waits into account, and is only available on a limited number of platforms. Event counting is available on all platforms and it provides the number of events that were received by the event loop before they are sent to their corresponding targets (this might include non-Qt events).
The Linux Performance Monitoring solution is available only on Linux and provides many different counters, which can be selected by passing an additional option -perfcounter countername
, such as
-perfcounter cache-misses
, -perfcounter branch-misses
, or -perfcounter l1d-load-misses
. The default counter is cpu-cycles
. The full list of counters can be obtained by running
any benchmark executable with the option -perfcounterlist
.
See Writing a Benchmark in the Qt Test Tutorial for more benchmarking examples.