1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5 \page qtquicktest-index.html
7 \brief Unit testing framework for QML.
9 \target Introduction to Qt Quick Test
10 \section1 Introduction
12 \l {Qt Quick Test QML Types}{Qt Quick Test} is a unit test framework for QML applications.
13 Test cases are written as JavaScript functions within a \l [QML] TestCase
23 function test_math() {
24 compare(2 + 2, 4, "2 + 2 = 4")
27 function test_fail() {
28 compare(2 + 2, 5, "2 + 2 = 5")
33 Functions whose names start with \c{test_} are treated as test cases
34 to be executed. See the documentation for the \l [QML] TestCase and
35 \l [QML] SignalSpy types for more information on writing test cases.
37 \note There is no binary compatibility guarantee for the Qt Quick Test
38 module. This means that an application that uses Qt Quick Test is
39 only guaranteed to work with the Qt version it was developed against.
40 However, source compatibility is guaranteed.
42 \section1 Using the Module
46 The QML types in Qt Quick Test are available through the \c QtTest import.
47 To use the types, add the following import statement to your .qml file:
55 Using the \l{Qt Quick Test C++ API}{C++ API} requires linking against the
56 module library, either directly or through other dependencies. Several
57 build tools have dedicated support for this, including
58 \l{CMake Documentation}{CMake} and \l{qmake}.
60 \section3 Building with CMake
62 Use the \c find_package() command to locate the needed module components in
65 \snippet overview.cmake cmake_use
67 See also the \l{Build with CMake} overview.
69 \section3 Building with qmake
71 There are two ways to link against the corresponding C++ library. If your
72 test project uses a QML \l TestCase, you should already have the following
73 line in your project file:
79 This will cause the test to link to the C++ \QtQuickTest library.
81 If you have a C++-only test project, you can add the following line
88 \target Running Qt Quick Tests
89 \section1 Running Tests
91 Test cases are launched by a C++ harness that consists of
94 \snippet src_qmltest_qquicktest_snippet.cpp 1
96 Where "example" is the identifier to use to uniquely identify
97 this set of tests. Finally, add \c{CONFIG += qmltestcase} to the project
103 CONFIG += warn_on qmltestcase
104 SOURCES += tst_example.cpp
107 The test harness scans the specified source directory recursively
108 for "tst_*.qml" files. If \c{QUICK_TEST_SOURCE_DIR} is not defined,
109 then the current directory will be scanned when the harness is run.
110 Other *.qml files may appear for auxillary QML components that are
113 The \c{-input} command-line option can be set at runtime to run
114 test cases from a different directory. This may be needed to run
115 tests on a target device where the compiled-in directory name refers
116 to a host. For example:
119 tst_example -input /mnt/SDCard/qmltests
122 It is also possible to run a single file using the \c{-input} option.
126 tst_example -input data/test.qml
130 tst_example -input <full_path>/test.qml
133 \note Specifying the full path to the qml test file is for example
134 needed for shadow builds.
136 If your test case needs QML imports, then you can add them as
137 \c{-import} options to the test program command-line.
139 If \c IMPORTPATH is specified in your .pro file, each import path added to \c IMPORTPATH
140 will be passed as a command-line argument when the test is run using "make check":
143 IMPORTPATH += $$PWD/../imports/my_module1 $$PWD/../imports/my_module2
146 The \c{-functions} command-line option will return a list of the current
147 tests functions. It is possible to run a single test function using the name
148 of the test function as an argument. For example:
151 tst_example Test_Name::function1
154 The \c{-help} command-line option will return all the options available.
160 \note Running a Qt Quick test case will always show a window on the screen,
161 even if the test code doesn't involve any Quick UI. To avoid that, run the
162 test executable with \c {-platform offscreen}.
164 \section1 Executing C++ Before QML Tests
166 To execute C++ code before any of the QML tests are run, the
167 \l QUICK_TEST_MAIN_WITH_SETUP macro can be used. This can be useful for
168 setting context properties on the QML engine, amongst other things.
170 The macro is identical to \c QUICK_TEST_MAIN, except that it takes an
171 additional \c QObject* argument. The test framework will call slots and
172 invokable functions with the following names:
180 \li \c {void applicationAvailable()}
181 \li Called right after the QApplication object was instantiated.
182 Use this function to perform setup that does not require a
183 \l QQmlEngine instance.
186 \li \c {void qmlEngineAvailable(QQmlEngine *)}
187 \li Called when the QML engine is available.
188 Any \l {QQmlEngine::addImportPath}{import paths},
189 \l {QQmlEngine::addPluginPath}{plugin paths},
190 and \l {QQmlFileSelector::setExtraSelectors}{extra file selectors}
191 will have been set on the engine by this point.
193 This function is called once for each QML test file,
194 so any arguments are unique to that test. For example, this
195 means that each QML test file will have its own QML engine.
197 This function can be used to \l {Choosing the Correct Integration
198 Method Between C++ and QML}{register QML types} and
199 \l {QQmlEngine::addImportPath()}{add import paths},
200 amongst other things.
203 \li \c {void cleanupTestCase()}
204 \li Called right after the test execution has finished.
205 Use this function to clean up before everything will start to be destructed.
209 The following example demonstrates how the macro can be used to set context
210 properties on the QML engine:
212 \snippet src_qmltest_qquicktest.cpp 2
214 The \c .moc include is based on the file name of the \c .cpp file.
215 For example, in the example above, the \c .cpp file is named
216 \c src_qmltest_qquicktest.cpp. If the file was named \c MyTest.cpp, the include would
220 #include "MyTest.moc"
226 \li \l{Qt Quick Test QML Types}{QML Types}
227 \li \l{Qt Quick Test C++ API}{C++ API}
232 Qt Quick Tests is available under commercial licenses from \l{The Qt Company}.
233 In addition, it is available under free software licenses. Since Qt 5.4,
234 these free software licenses are
235 \l{GNU Lesser General Public License, version 3}, or
236 the \l{GNU General Public License, version 2}.
237 See \l{Qt Licensing} for further details.