Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qtquicktest-index.qdoc
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtquicktest-index.html
6 \title Qt Quick Test
7 \brief Unit testing framework for QML.
8
9 \target Introduction to Qt Quick Test
10 \section1 Introduction
11
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
14 type:
15
16 \qml
17 import QtQuick 2.3
18 import QtTest 1.0
19
20 TestCase {
21 name: "MathTests"
22
23 function test_math() {
24 compare(2 + 2, 4, "2 + 2 = 4")
25 }
26
27 function test_fail() {
28 compare(2 + 2, 5, "2 + 2 = 5")
29 }
30 }
31 \endqml
32
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.
36
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.
41
42 \section1 Using the Module
43
44 \section2 QML API
45
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:
48
49 \qml
50 import QtTest
51 \endqml
52
53 \section2 C++ API
54
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}.
59
60 \section3 Building with CMake
61
62 Use the \c find_package() command to locate the needed module components in
63 the Qt6 package:
64
65 \snippet overview.cmake cmake_use
66
67 See also the \l{Build with CMake} overview.
68
69 \section3 Building with qmake
70
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:
74
75 \badcode
76 CONFIG += qmltestcase
77 \endcode
78
79 This will cause the test to link to the C++ \QtQuickTest library.
80
81 If you have a C++-only test project, you can add the following line
82 to your project file:
83
84 \badcode
85 QT += qmltest
86 \endcode
87
88 \target Running Qt Quick Tests
89 \section1 Running Tests
90
91 Test cases are launched by a C++ harness that consists of
92 the following code:
93
94 \snippet src_qmltest_qquicktest_snippet.cpp 1
95
96 Where "example" is the identifier to use to uniquely identify
97 this set of tests. Finally, add \c{CONFIG += qmltestcase} to the project
98 file:
99
100 \badcode
101 TEMPLATE = app
102 TARGET = tst_example
103 CONFIG += warn_on qmltestcase
104 SOURCES += tst_example.cpp
105 \endcode
106
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
111 used by the test.
112
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:
117
118 \badcode
119 tst_example -input /mnt/SDCard/qmltests
120 \endcode
121
122 It is also possible to run a single file using the \c{-input} option.
123 For example:
124
125 \badcode
126 tst_example -input data/test.qml
127 \endcode
128
129 \badcode
130 tst_example -input <full_path>/test.qml
131 \endcode
132
133 \note Specifying the full path to the qml test file is for example
134 needed for shadow builds.
135
136 If your test case needs QML imports, then you can add them as
137 \c{-import} options to the test program command-line.
138
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":
141
142 \badcode
143 IMPORTPATH += $$PWD/../imports/my_module1 $$PWD/../imports/my_module2
144 \endcode
145
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:
149
150 \badcode
151 tst_example Test_Name::function1
152 \endcode
153
154 The \c{-help} command-line option will return all the options available.
155
156 \badcode
157 tst_example -help
158 \endcode
159
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}.
163
164 \section1 Executing C++ Before QML Tests
165
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.
169
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:
173
174 \table
175 \header
176 \li Name
177 \li Purpose
178 \li Since
179 \row
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.
184 \li Qt 5.12
185 \row
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.
192
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.
196
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.
201 \li Qt 5.11
202 \row
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.
206 \li Qt 5.12
207 \endtable
208
209 The following example demonstrates how the macro can be used to set context
210 properties on the QML engine:
211
212 \snippet src_qmltest_qquicktest.cpp 2
213
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
217 be:
218
219 \code
220 #include "MyTest.moc"
221 \endcode
222
223 \section1 Reference
224
225 \list
226 \li \l{Qt Quick Test QML Types}{QML Types}
227 \li \l{Qt Quick Test C++ API}{C++ API}
228 \endlist
229
230 \section1 Licenses
231
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.
238*/