source: trunk/doc/src/development/qtestlib.qdoc@ 788

Last change on this file since 788 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 28.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page qtestlib-manual.html
44 \title QTestLib Manual
45 \brief An overview of Qt's unit testing framework.
46
47 \ingroup frameworks-technologies
48 \keyword qtestlib
49
50 The QTestLib framework, provided by Nokia, is a tool for unit
51 testing Qt based applications and libraries. QTestLib provides
52 all the functionality commonly found in unit testing frameworks as
53 well as extensions for testing graphical user interfaces.
54
55 Table of contents:
56
57 \tableofcontents
58
59 \section1 QTestLib Features
60
61 QTestLib is designed to ease the writing of unit tests for Qt
62 based applications and libraries:
63
64 \table
65 \header \o Feature \o Details
66 \row
67 \o \bold Lightweight
68 \o QTestLib consists of about 6000 lines of code and 60
69 exported symbols.
70 \row
71 \o \bold Self-contained
72 \o QTestLib requires only a few symbols from the Qt Core library
73 for non-gui testing.
74 \row
75 \o \bold {Rapid testing}
76 \o QTestLib needs no special test-runners; no special
77 registration for tests.
78 \row
79 \o \bold {Data-driven testing}
80 \o A test can be executed multiple times with different test data.
81 \row
82 \o \bold {Basic GUI testing}
83 \o QTestLib offers functionality for mouse and keyboard simulation.
84 \row
85 \o \bold {Benchmarking}
86 \o QTestLib supports benchmarking and provides several measurement back-ends.
87 \row
88 \o \bold {IDE friendly}
89 \o QTestLib outputs messages that can be interpreted by Visual
90 Studio and KDevelop.
91 \row
92 \o \bold Thread-safety
93 \o The error reporting is thread safe and atomic.
94 \row
95 \o \bold Type-safety
96 \o Extensive use of templates prevent errors introduced by
97 implicit type casting.
98 \row
99 \o \bold {Easily extendable}
100 \o Custom types can easily be added to the test data and test output.
101 \endtable
102
103 Note: For higher-level GUI and application testing needs, please
104 see the \l{Third-Party Tools}{Qt testing products provided by
105 Nokia partners}.
106
107
108 \section1 QTestLib API
109
110 All public methods are in the \l QTest namespace. In addition, the
111 \l QSignalSpy class provides easy introspection for Qt's signals and slots.
112
113
114 \section1 Using QTestLib
115
116 \section2 Creating a Test
117
118 To create a test, subclass QObject and add one or more private slots to it. Each
119 private slot is a testfunction in your test. QTest::qExec() can be used to execute
120 all testfunctions in the test object.
121
122 In addition, there are four private slots that are \e not treated as testfunctions.
123 They will be executed by the testing framework and can be used to initialize and
124 clean up either the entire test or the current test function.
125
126 \list
127 \o \c{initTestCase()} will be called before the first testfunction is executed.
128 \o \c{cleanupTestCase()} will be called after the last testfunction was executed.
129 \o \c{init()} will be called before each testfunction is executed.
130 \o \c{cleanup()} will be called after every testfunction.
131 \endlist
132
133 If \c{initTestCase()} fails, no testfunction will be executed. If \c{init()} fails,
134 the following testfunction will not be executed, the test will proceed to the next
135 testfunction.
136
137 Example:
138 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 0
139
140 For more examples, refer to the \l{QTestLib Tutorial}.
141
142 \section2 Building a Test
143
144 If you are using \c qmake as your build tool, just add the
145 following to your project file:
146
147 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 1
148
149 If you are using other buildtools, make sure that you add the location
150 of the QTestLib header files to your include path (usually \c{include/QtTest}
151 under your Qt installation directory). If you are using a release build
152 of Qt, link your test to the \c QtTest library. For debug builds, use
153 \c{QtTest_debug}.
154
155 See \l {Chapter 1: Writing a Unit Test}{Writing a Unit Test} for a step by
156 step explanation.
157
158 \section2 QTestLib Command Line Arguments
159
160 \section3 Syntax
161
162 The syntax to execute an autotest takes the following simple form:
163
164 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 2
165
166 Substitute \c testname with the name of your executable. \c
167 testfunctions can contain names of test functions to be
168 executed. If no \c testfunctions are passed, all tests are run. If you
169 append the name of an entry in \c testdata, the test function will be
170 run only with that test data.
171
172 For example:
173
174 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 3
175
176 Runs the test function called \c toUpper with all available test data.
177
178 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 4
179
180 Runs the \c toUpper test function with all available test data,
181 and the \c toInt test function with the testdata called \c
182 zero (if the specified test data doesn't exist, the associated test
183 will fail).
184
185 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 5
186
187 Runs the testMyWidget function test, outputs every signal
188 emission and waits 500 milliseconds after each simulated
189 mouse/keyboard event.
190
191 \section3 Options
192
193 The following command line arguments are understood:
194
195 \list
196 \o \c -help \BR
197 outputs the possible command line arguments and give some useful help.
198 \o \c -functions \BR
199 outputs all test functions available in the test.
200 \o \c -o \e filename \BR
201 write output to the specified file, rather than to standard output
202 \o \c -silent \BR
203 silent output, only shows warnings, failures and minimal status messages
204 \o \c -v1 \BR
205 verbose output; outputs information on entering and exiting test functions.
206 \o \c -v2 \BR
207 extended verbose output; also outputs each \l QCOMPARE() and \l QVERIFY()
208 \o \c -vs \BR
209 outputs every signal that gets emitted
210 \o \c -xml \BR
211 outputs XML formatted results instead of plain text
212 \o \c -lightxml \BR
213 outputs results as a stream of XML tags
214 \o \c -eventdelay \e ms \BR
215 if no delay is specified for keyboard or mouse simulation
216 (\l QTest::keyClick(),
217 \l QTest::mouseClick() etc.), the value from this parameter
218 (in milliseconds) is substituted.
219 \o \c -keydelay \e ms \BR
220 like -eventdelay, but only influences keyboard simulation and not mouse
221 simulation.
222 \o \c -mousedelay \e ms \BR
223 like -eventdelay, but only influences mouse simulation and not keyboard
224 simulation.
225 \o \c -keyevent-verbose \BR
226 output more verbose output for keyboard simulation
227 \o \c -maxwarnings \e number\BR
228 sets the maximum number of warnings to output. 0 for unlimited, defaults to 2000.
229 \endlist
230
231 \section2 Creating a Benchmark
232
233 To create a benchmark, follow the instructions for crating a test and then add a
234 QBENCHMARK macro to the test function that you want to benchmark.
235
236 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 12
237
238 The code insde the QBENCHMARK macro will be measured, and possibly also repeated
239 several times in order to get an accurate measurement. This depends on the selected
240 measurement back-end. Several back-ends are available. They can be selected on the
241 command line:
242
243 \target testlib-benchmarking-measurement
244
245 \table
246 \header \o Name
247 \o Commmand-line Argument
248 \o Availability
249 \row \o Walltime
250 \o (default)
251 \o All platforms
252 \row \o CPU tick counter
253 \o -tickcounter
254 \o Windows, Mac OS X, Linux, many UNIX-like systems.
255 \row \o Valgrind/Callgrind
256 \o -callgrind
257 \o Linux (if installed)
258 \row \o Event Counter
259 \o -eventcounter
260 \o All platforms
261 \endtable
262
263 In short, walltime is always available but requires many repetitions to
264 get a useful result.
265 Tick counters are usually available and can provide
266 results with fewer repetitions, but can be susceptible to CPU frequency
267 scaling issues.
268 Valgrind provides exact results, but does not take
269 I/O waits into account, and is only available on a limited number of
270 platforms.
271 Event counting is available on all platforms and it provides the number of events
272 that were received by the event loop before they are sent to their corresponding
273 targets (this might include non-Qt events).
274
275 \note Depending on the device configuration, Tick counters on the
276 Windows CE platform may not be as fine-grained, compared to other platforms.
277 Devices that do not support high-resolution timers default to
278 one-millisecond granularity.
279
280 See the chapter 5 in the \l{QTestLib Tutorial} for more benchmarking examples.
281
282 \section1 Using QTestLib remotely on Windows CE
283 \c cetest is a convenience application which helps the user to launch an
284 application remotely on a Windows CE device or emulator.
285
286 It needs to be executed after the unit test has been successfully compiled.
287
288 Prior to launching, the following files are copied to the device:
289
290 \list
291 \o all Qt libraries the project links to
292 \o \l {QtRemote}{QtRemote.dll}
293 \o the c runtime library specified during installation
294 \o all files specified in the \c .pro file following the \l DEPLOYMENT rules.
295 \endlist
296
297 \section2 Using \c cetest
298 \section3 Syntax
299 The syntax to execute an autotest takes the following simple form:
300
301 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 6
302
303 \section3 Options
304 \c cetest provides the same options as those for unit-testing on non cross-compiled
305 platforms. See \l {QTestLib Command Line Arguments} {Command Line Arguments} for
306 more information.
307
308 The following commands are also included:
309
310 \list
311 \o \c -debug \BR
312 Test version compiled in debug mode.
313 \o \c -release \BR
314 Test version compiled in release mode.
315 \o \c -libpath \e path \BR
316 Target path to copy Qt libraries to.
317 \o \c -qt-delete \BR
318 Delete Qt libraries after execution.
319 \o \c -project-delete \BR
320 Delete project files after execution.
321 \o \c -delete \BR
322 Delete project and Qt libraries after execution.
323 \o \c -conf \BR
324 Specifies a qt.conf file to be deployed to remote directory.
325 \endlist
326
327 \note \c{debug} is the default build option.
328
329 \section2 QtRemote
330 \c QtRemote is a small library which is build after QTestLib. It allows the host
331 system to create a process on a remote device and waits until its execution has
332 been finished.
333
334 \section2 Requirements
335 \c cetest uses Microsoft ActiveSync to establish a remote connection between the
336 host computer and the device. Thus header files and libraries are needed to compile
337 cetest and QtRemote successfully.
338
339 Prior to \l{Installing Qt on Windows CE}{installation} of Qt, you need to set your
340 \c INCLUDE and \c LIB environment variables properly.
341
342 A default installation of Windows Mobile 5 for Pocket PC can be obtained by:
343
344 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 7
345
346 Note that Qt will remember the path, so you do not need to set it again
347 after switching the environments for cross-compilation.
348
349 \section1 3rd Party Code
350
351 The CPU tick counters used for benchmarking is licensed under the following
352 license: (from src/testlib/3rdparty/cycle.h)
353
354 \legalese
355 Copyright (c) 2003, 2006 Matteo Frigo\br
356 Copyright (c) 2003, 2006 Massachusetts Institute of Technology