Qt Quick Test
Introduction
Qt Quick Test is a unit test framework for QML applications. Test cases are written as JavaScript functions within a TestCase type:
import QtQuick 2.3 import QtTest 1.0 TestCase { name: "MathTests" function test_math() { compare(2 + 2, 4, "2 + 2 = 4") } function test_fail() { compare(2 + 2, 5, "2 + 2 = 5") } }
Functions whose names start with test_
are treated as test cases to be executed. See the documentation for the TestCase and SignalSpy types for more information on writing test cases.
Note: There is no binary compatibility guarantee for the Qt Quick Test module. This means that an application that uses Qt Quick Test is only guaranteed to work with the Qt version it was developed against. However, source compatibility is guaranteed.
Running Tests
Test cases are launched by a C++ harness that consists of the following code:
#include <QtQuickTest/quicktest.h>
QUICK_TEST_MAIN(example)
Where "example" is the identifier to use to uniquely identify this set of tests. Finally, add CONFIG += qmltestcase
to the project file:
TEMPLATE = app TARGET = tst_example CONFIG += warn_on qmltestcase SOURCES += tst_example.cpp
The test harness scans the specified source directory recursively for "tst_*.qml" files. If QUICK_TEST_SOURCE_DIR
is not defined, then the current directory will be scanned when the harness is run. Other *.qml files may appear for auxillary QML components that are used by the test.
The -input
command-line option can be set at runtime to run test cases from a different directory. This may be needed to run tests on a target device where the compiled-in directory name refers to a host. For example:
tst_example -input /mnt/SDCard/qmltests
It is also possible to run a single file using the -input
option. For example:
tst_example -input data/test.qml
tst_example -input <full_path>/test.qml
Note: Specifying the full path to the qml test file is for example needed for shadow builds.
If your test case needs QML imports, then you can add them as -import
options to the test program command-line.
If IMPORTPATH
is specified in your .pro file, each import path added to IMPORTPATH
will be passed as a command-line argument when the test is run using "make check":
IMPORTPATH += $$PWD/../imports/my_module1 $$PWD/../imports/my_module2
The -functions
command-line option will return a list of the current tests functions. It is possible to run a single test function using the name of the test function as an argument. For example:
tst_example Test_Name::function1
The -help
command-line option will return all the options available.
tst_example -help
Executing C++ Before QML Tests
To execute C++ code before any of the QML tests are run, the QUICK_TEST_MAIN_WITH_SETUP
macro can be used. This can be useful for setting context properties on the QML engine, amongst other things.
The macro is identical to QUICK_TEST_MAIN
, except that it takes an additional QObject*
argument. The test framework will call slots and invokable functions with the following names:
Name | Purpose | Since |
---|---|---|
void applicationAvailable() | Called right after the QApplication object was instantiated. Use this function to perform setup that does not require a QQmlEngine instance. | Qt 5.12 |
void qmlEngineAvailable(QQmlEngine*) | Called when the QML engine is available. Any import paths, plugin paths, and extra file selectors will have been set on the engine by this point. This function can be used to register QML types and add import paths, amongst other things. | Qt 5.11 |
void cleanupTestCase() | Called right after the test execution has finished. Use this function to clean up before everything will start to be destructed. | Qt 5.12 |
Each function will be called once for each tst_*.qml
file, so any arguments are unique to that test. For example, this means that each QML test file will have its own QML engine.
The following example demonstrates how the macro can be used to set context properties on the QML engine:
#include <QtQuickTest> #include <QQmlEngine> #include <QQmlContext> class Setup : public QObject { Q_OBJECT public: Setup() {} public slots: void qmlEngineAvailable(QQmlEngine *engine) { engine->rootContext()->setContextProperty("myContextProperty", QVariant(true)); } }; QUICK_TEST_MAIN_WITH_SETUP(mytest, Setup) #include "tst_mytest.moc"
The .moc
include is based on the file name of the .cpp
file. For example, in the example above, the .cpp
file is named tst_mytest.cpp
. If the file was named MyTest.cpp
, the include would be:
#include "MyTest.moc"
Licenses
Qt Quick Tests is available under commercial licenses from The Qt Company. In addition, it is available under free software licenses. Since Qt 5.4, these free software licenses are GNU Lesser General Public License, version 3, or the GNU General Public License, version 2. See Qt Licensing for further details.
© 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.