Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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 Many of the best practices described in \l{Qt Test} also apply to Qt Quick
38 Test. These are covered in \l {Qt Test Best Practices}.
39
40 \note There is no binary compatibility guarantee for the Qt Quick Test
41 module. This means that an application that uses Qt Quick Test is
42 only guaranteed to work with the Qt version it was developed against.
43 However, source compatibility is guaranteed.
44
45 \section1 Using the Module
46
47 \section2 QML API
48
49 The QML types in Qt Quick Test are available through the \c QtTest import.
50 To use the types, add the following import statement to your .qml file:
51
52 \qml
53 import QtTest
54 \endqml
55
56 \section2 C++ API
57
58 Using the \l{Qt Quick Test C++ API}{C++ API} requires linking against the
59 module library, either directly or through other dependencies. Several
60 build tools have dedicated support for this, including
61 \l{CMake Documentation}{CMake} and \l{qmake}.
62
63 \section3 Building with CMake
64
65 Use the \c find_package() command to locate the needed module components in
66 the Qt6 package:
67
68 \snippet overview.cmake cmake_use
69
70 See also the \l{Build with CMake} overview.
71
72 \section3 Building with qmake
73
74 There are two ways to link against the corresponding C++ library. If your
75 test project uses a QML \l TestCase, you should already have the following
76 line in your project file:
77
78 \badcode
79 CONFIG += qmltestcase
80 \endcode
81
82 This will cause the test to link to the C++ \c QtQuickTest library.
83
84 If you have a C++-only test project, you can add the following line
85 to your project file:
86
87 \badcode
88 QT += qmltest
89 \endcode
90
91 \target Running Qt Quick Tests
92 \section1 Running Tests
93
94 Test cases are launched by a C++ harness that consists of
95 the following code:
96
97 \snippet src_qmltest_qquicktest_snippet.cpp 1
98
99 Where "example" is the identifier to use to uniquely identify
100 this set of tests.
101
102 \if defined(onlinedocs)
103 \tab {run-qtquicktest}{tab-cmake}{CMake}{checked}
104 \tab {run-qtquicktest}{tab-qmake}{qmake}{}
105 \tabcontent {tab-cmake}
106 \else
107 \section1 Using CMake
108 \endif
109 Configure your CMakeLists.txt file and build your project using your
110 favorite generator.
111 \badcode
112 cmake_minimum_required(VERSION 3.2)
113
114 project(tst_example LANGUAGES CXX)
115
116 enable_testing()
117
118 find_package(Qt6 REQUIRED COMPONENTS QuickTest Qml)
119
120 #[[The test harness scans the specified source directory recursively
121 for "tst_*.qml" files. By default, it looks in the current directory,
122 which is usually where the executable is. This command makes it look
123 in the project's source directory instead.]]
124 add_definitions(-DQUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
125
126 qt_standard_project_setup(REQUIRES 6.6)
127
128 add_executable(tst_example tst_example.cpp)
129
130 add_test(NAME tst_example COMMAND tst_example)
131
132 target_link_libraries(tst_example
133 PRIVATE
134 Qt6::QuickTest
135 Qt6::Qml
136 )
137 \endcode
138 \if defined(onlinedocs)
139 \endtabcontent
140 \tabcontent {tab-qmake}
141 \else
142 \section1 Using qmake
143 \endif
144 Add \c{CONFIG += qmltestcase} to your project file:
145 \badcode
146 TEMPLATE = app
147 TARGET = tst_example
148 CONFIG += warn_on qmltestcase
149 SOURCES += tst_example.cpp
150 \endcode
151
152 If \c IMPORTPATH is specified in your .pro file, each import path added to \c IMPORTPATH
153 will be passed as a command-line argument when the test is run using "make check":
154
155 \badcode
156 IMPORTPATH += $$PWD/../imports/my_module1 $$PWD/../imports/my_module2
157 \endcode
158 \if defined(onlinedocs)
159 \endtabcontent
160 \endif
161
162 The test harness scans the specified source directory recursively
163 for "tst_*.qml" files. If \c{QUICK_TEST_SOURCE_DIR} is not defined,
164 then the current directory will be scanned when the harness is run.
165 Other *.qml files may appear for auxillary QML components that are
166 used by the test.
167
168 The \c{-input} command-line option can be set at runtime to run
169 test cases from a different directory. This may be needed to run
170 tests on a target device where the compiled-in directory name refers
171 to a host. For example:
172
173 \badcode
174 tst_example -input /mnt/SDCard/qmltests
175 \endcode
176
177 It is also possible to run a single file using the \c{-input} option.
178 For example:
179
180 \badcode
181 tst_example -input data/test.qml
182 \endcode
183
184 \badcode
185 tst_example -input <full_path>/test.qml
186 \endcode
187
188 \note Specifying the full path to the qml test file is for example
189 needed for shadow builds.
190
191 If your test case needs QML imports, then you can add them as
192 \c{-import} options to the test program command-line.
193
194
195 The \c{-functions} command-line option will return a list of the current
196 tests functions. It is possible to run a single test function using the name
197 of the test function as an argument. For example:
198
199 \badcode
200 tst_example Test_Name::function1
201 \endcode
202
203 The \c{-help} command-line option will return all the options available.
204
205 \badcode
206 tst_example -help
207 \endcode
208
209 \note Running a Qt Quick test case will always show a window on the screen,
210 even if the test code doesn't involve any Quick UI. To avoid that, run the
211 test executable with \c {-platform offscreen}.
212
213 \section1 Executing C++ Before QML Tests
214
215 To execute C++ code before any of the QML tests are run, the
216 \l QUICK_TEST_MAIN_WITH_SETUP macro can be used. This can be useful for
217 setting context properties on the QML engine, amongst other things.
218
219 The macro is identical to \c QUICK_TEST_MAIN, except that it takes an
220 additional type argument. The test framework will call slots and
221 invokable functions with the following names:
222
223 \table
224 \header
225 \li Name
226 \li Purpose
227 \li Since
228 \row
229 \li \c {void applicationAvailable()}
230 \li Called right after the QApplication object was instantiated.
231 Use this function to perform setup that does not require a
232 \l QQmlEngine instance.
233 \li Qt 5.12
234 \row
235 \li \c {void qmlEngineAvailable(QQmlEngine *)}
236 \li Called when the QML engine is available.
237 Any \l {QQmlEngine::addImportPath}{import paths},
238 \l {QQmlEngine::addPluginPath}{plugin paths},
239 and \l {QQmlFileSelector::setExtraSelectors}{extra file selectors}
240 will have been set on the engine by this point.
241
242 This function is called once for each QML test file,
243 so any arguments are unique to that test. For example, this
244 means that each QML test file will have its own QML engine.
245
246 This function can be used to \l {Choosing the Correct Integration
247 Method Between C++ and QML}{register QML types} and
248 \l {QQmlEngine::addImportPath()}{add import paths},
249 amongst other things.
250 \li Qt 5.11
251 \row
252 \li \c {void cleanupTestCase()}
253 \li Called right after the test execution has finished.
254 Use this function to clean up before everything will start to be destructed.
255 \li Qt 5.12
256 \endtable
257
258 The following example demonstrates how the macro can be used to set context
259 properties on the QML engine:
260
261 \snippet src_qmltest_qquicktest.cpp 2
262
263 The \c .moc include is based on the file name of the \c .cpp file.
264 For example, in the example above, the \c .cpp file is named
265 \c src_qmltest_qquicktest.cpp. If the file was named \c MyTest.cpp, the include would
266 be:
267
268 \code
269 #include "MyTest.moc"
270 \endcode
271
272 \section1 Reference
273
274 \list
275 \li \l{Qt Quick Test QML Types}{QML Types}
276 \li \l{Qt Quick Test C++ API}{C++ API}
277 \endlist
278
279 \section1 Licenses
280
281 Qt Quick Tests is available under commercial licenses from \l{The Qt Company}.
282 In addition, it is available under free software licenses. Since Qt 5.4,
283 these free software licenses are
284 \l{GNU Lesser General Public License, version 3}, or
285 the \l{GNU General Public License, version 2}.
286 See \l{Qt Licensing} for further details.
287*/