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

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

trunk: Merged in qt 4.6.2 sources.

  • 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 an 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 Arguemnt
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
357
358 Permission is hereby granted, free of charge, to any person obtaining
359 a copy of this software and associated documentation files (the
360 "Software"), to deal in the Software without restriction, including
361 without limitation the rights to use, copy, modify, merge, publish,
362 distribute, sublicense, and/or sell copies of the Software, and to
363 permit persons to whom the Software is furnished to do so, subject to
364 the following conditions:
365
366 The above copyright notice and this permission notice shall be
367 included in all copies or substantial portions of the Software.
368
369 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
370 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
371 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
372 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
373 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
374 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
375 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
376 \endlegalese
377*/
378
379/*!
380 \page qtestlib-tutorial.html
381 \brief A short introduction to testing with QTestLib.
382 \contentspage QTestLib Manual
383 \nextpage {Chapter 1: Writing a Unit Test}{Chapter 1}
384
385 \title QTestLib Tutorial
386
387 This tutorial gives a short introduction to how to use some of the
388 features of the QTestLib framework. It is divided into four
389 chapters:
390
391 \list 1
392 \o \l {Chapter 1: Writing a Unit Test}{Writing a Unit Test}
393 \o \l {Chapter 2: Data Driven Testing}{Data Driven Testing}
394 \o \l {Chapter 3: Simulating GUI Events}{Simulating GUI Events}
395 \o \l {Chapter 4: Replaying GUI Events}{Replaying GUI Events}
396 \o \l {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
397 \endlist
398
399*/
400
401
402/*!
403 \example qtestlib/tutorial1
404
405 \contentspage {QTestLib Tutorial}{Contents}
406 \nextpage {Chapter 2: Data Driven Testing}{Chapter 2}
407
408 \title Chapter 1: Writing a Unit Test
409
410 In this first chapter we will see how to write a simple unit test
411 for a class, and how to execute it.
412
413 \section1 Writing a Test
414
415 Let's assume you want to test the behavior of our QString class.
416 First, you need a class that contains your test functions. This class
417 has to inherit from QObject:
418
419 \snippet examples/qtestlib/tutorial1/testqstring.cpp 0
420
421 Note that you need to include the QTest header, and that the
422 test functions have to be declared as private slots so the
423 test framework finds and executes it.
424
425 Then you need to implement the test function itself. The
426 implementation could look like this:
427
428 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 8
429
430 The \l QVERIFY() macro evaluates the expression passed as its
431 argument. If the expression evaluates to true, the execution of
432 the test function continues. Otherwise, a message describing the
433 failure is appended to the test log, and the test function stops
434 executing.
435
436 But if you want a more verbose output to the test log, you should
437 use the \l QCOMPARE() macro instead:
438
439 \snippet examples/qtestlib/tutorial1/testqstring.cpp 1
440
441 If the strings are not equal, the contents of both strings is
442 appended to the test log, making it immediately visible why the
443 comparison failed.
444
445 Finally, to make our test case a stand-alone executable, the
446 following two lines are needed:
447
448 \snippet examples/qtestlib/tutorial1/testqstring.cpp 2
449
450 The \l QTEST_MAIN() macro expands to a simple \c main()
451 method that runs all the test functions. Note that if both the
452 declaration and the implementation of our test class are in a \c
453 .cpp file, we also need to include the generated moc file to make
454 Qt's introspection work.
455
456 \section1 Executing a Test
457
458 Now that we finished writing our test, we want to execute
459 it. Assuming that our test was saved as \c testqstring.cpp in an
460 empty directory: we build the test using qmake to create a project
461 and generate a makefile.
462
463 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 9
464
465 \bold {Note:}If you're using windows, replace \c make with \c
466 nmake or whatever build tool you use.
467
468 Running the resulting executable should give you the following
469 output:
470
471 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 10
472
473 Congratulations! You just wrote and executed your first unit test
474 using the QTestLib framework.
475*/
476
477/*!
478 \example qtestlib/tutorial2
479
480 \previouspage {Chapter 1: Writing a Unit Test}{Chapter 1}
481 \contentspage {QTestLib Tutorial}{Contents}
482 \nextpage {Chapter 3: Simulating Gui Events}{Chapter 3}
483
484 \title Chapter 2: Data Driven Testing
485
486 In this chapter we will demonstrate how to execute a test
487 multiple times with different test data.
488
489 So far, we have hard coded the data we wanted to test into our
490 test function. If we add more test data, the function might look like
491 this:
492
493 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 11
494
495 To prevent that the function ends up being cluttered by repetitive
496 code, QTestLib supports adding test data to a test function. All
497 we need is to add another private slot to our test class:
498
499 \snippet examples/qtestlib/tutorial2/testqstring.cpp 0
500
501 \section1 Writing the Data Function
502
503 A test function's associated data function carries the same name,
504 appended by \c{_data}. Our data function looks like this:
505
506 \snippet examples/qtestlib/tutorial2/testqstring.cpp 1
507
508 First, we define the two elements of our test table using the \l
509 QTest::addColumn() function: A test string, and the
510 expected result of applying the QString::toUpper() function to
511 that string.
512
513 Then we add some data to the table using the \l
514 QTest::newRow() function. Each set of data will become a
515 separate row in the test table.
516
517 \l QTest::newRow() takes one argument: A name that will be
518 associated with the data set. If the test fails, the name will be
519 used in the test log, referencing the failed data. Then we
520 stream the data set into the new table row: First an arbitrary
521 string, and then the expected result of applying the
522 QString::toUpper() function to that string.
523
524 You can think of the test data as a two-dimensional table. In
525 our case, it has two columns called \c string and \c result and
526 three rows. In addition a name as well as an index is associated
527 with each row:
528
529 \table
530 \header
531 \o index
532 \o name
533 \o string
534 \o result
535 \row
536 \o 0
537 \o all lower
538 \o "hello"
539 \o HELLO
540 \row
541 \o 1
542 \o mixed
543 \o "Hello"
544 \o HELLO
545 \row
546 \o 2
547 \o all upper
548 \o "HELLO"
549 \o HELLO
550 \endtable
551
552 \section1 Rewriting the Test Function
553
554 Our test function can now be rewritten:
555
556 \snippet examples/qtestlib/tutorial2/testqstring.cpp 2
557
558 The TestQString::toUpper() function will be executed three times,
559 once for each entry in the test table that we created in the
560 associated TestQString::toUpper_data() function.
561
562 First, we fetch the two elements of the data set using the \l
563 QFETCH() macro. \l QFETCH() takes two arguments: The data type of
564 the element and the element name. Then we perform the test using
565 the \l QCOMPARE() macro.
566
567 This approach makes it very easy to add new data to the test
568 without modifying the test itself.
569
570 And again, to make our test case a stand-alone executable,
571 the following two lines are needed:
572
573 \snippet examples/qtestlib/tutorial2/testqstring.cpp 3
574
575 As before, the QTEST_MAIN() macro expands to a simple main()
576 method that runs all the test functions, and since both the
577 declaration and the implementation of our test class are in a .cpp
578 file, we also need to include the generated moc file to make Qt's
579 introspection work.
580*/
581
582/*!
583 \example qtestlib/tutorial3
584
585 \previouspage {Chapter 2 Data Driven Testing}{Chapter 2}
586 \contentspage {QTestLib Tutorial}{Contents}
587 \nextpage {Chapter 4: Replaying GUI Events}{Chapter 4}
588
589 \title Chapter 3: Simulating GUI Events
590
591 QTestLib features some mechanisms to test graphical user
592 interfaces. Instead of simulating native window system events,
593 QTestLib sends internal Qt events. That means there are no
594 side-effects on the machine the tests are running on.
595
596 In this chapter we will se how to write a simple GUI test.
597
598 \section1 Writing a GUI test
599
600 This time, let's assume you want to test the behavior of our
601 QLineEdit class. As before, you will need a class that contains
602 your test function:
603
604 \snippet examples/qtestlib/tutorial3/testgui.cpp 0
605
606 The only difference is that you need to include the QtGui class
607 definitions in addition to the QTest namespace.
608
609 \snippet examples/qtestlib/tutorial3/testgui.cpp 1
610
611 In the implementation of the test function we first create a
612 QLineEdit. Then we simulate writing "hello world" in the line edit
613 using the \l QTest::keyClicks() function.
614
615 \note The widget must also be shown in order to correctly test keyboard
616 shortcuts.
617
618 QTest::keyClicks() simulates clicking a sequence of keys on a
619 widget. Optionally, a keyboard modifier can be specified as well
620 as a delay (in milliseconds) of the test after each key click. In
621 a similar way, you can use the QTest::keyClick(),
622 QTest::keyPress(), QTest::keyRelease(), QTest::mouseClick(),
623 QTest::mouseDClick(), QTest::mouseMove(), QTest::mousePress()
624 and QTest::mouseRelease() functions to simulate the associated
625 GUI events.
626
627 Finally, we use the \l QCOMPARE() macro to check if the line edit's
628 text is as expected.
629
630 As before, to make our test case a stand-alone executable, the
631 following two lines are needed:
632
633 \snippet examples/qtestlib/tutorial3/testgui.cpp 2
634
635 The QTEST_MAIN() macro expands to a simple main() method that
636 runs all the test functions, and since both the declaration and
637 the implementation of our test class are in a .cpp file, we also
638 need to include the generated moc file to make Qt's introspection
639 work.
640*/
641
642/*!
643 \example qtestlib/tutorial4
644
645 \previouspage {Chapter 3: Simulating GUI Event}{Chapter 3}
646 \contentspage {QTestLib Tutorial}{Contents}
647 \nextpage {Chapter 5: Writing a Benchmark}{Chapter 5}
648
649 \title Chapter 4: Replaying GUI Events
650
651 In this chapter, we will show how to simulate a GUI event,
652 and how to store a series of GUI events as well as replay them on
653 a widget.
654
655 The approach to storing a series of events and replay them, is
656 quite similar to the approach explained in \l {Chapter 2:
657 Data Driven Testing}{chapter 2}; all you need is to add a data
658 function to your test class:
659
660 \snippet examples/qtestlib/tutorial4/testgui.cpp 0
661
662 \section1 Writing the Data Function
663
664 As before, a test function's associated data function carries the
665 same name, appended by \c{_data}.
666
667 \snippet examples/qtestlib/tutorial4/testgui.cpp 1
668
669 First, we define the elements of the table using the
670 QTest::addColumn() function: A list of GUI events, and the
671 expected result of applying the list of events on a QWidget. Note
672 that the type of the first element is \l QTestEventList.
673
674 A QTestEventList can be populated with GUI events that can be
675 stored as test data for later usage, or be replayed on any
676 QWidget.
677
678 In our current data function, we create two \l
679 {QTestEventList}s. The first list consists of a single click to
680 the 'a' key. We add the event to the list using the
681 QTestEventList::addKeyClick() function. Then we use the
682 QTest::newRow() function to give the data set a name, and
683 stream the event list and the expected result into the table.
684
685 The second list consists of two key clicks: an 'a' with a
686 following 'backspace'. Again we use the
687 QTestEventList::addKeyClick() to add the events to the list, and
688 QTest::newRow() to put the event list and the expected
689 result into the table with an associated name.
690
691 \section1 Rewriting the Test Function
692
693 Our test can now be rewritten:
694
695 \snippet examples/qtestlib/tutorial4/testgui.cpp 2
696
697 The TestGui::testGui() function will be executed two times,
698 once for each entry in the test data that we created in the
699 associated TestGui::testGui_data() function.
700
701 First, we fetch the two elements of the data set using the \l
702 QFETCH() macro. \l QFETCH() takes two arguments: The data type of
703 the element and the element name. Then we create a QLineEdit, and
704 apply the list of events on that widget using the
705 QTestEventList::simulate() function.
706
707 Finally, we use the QCOMPARE() macro to check if the line edit's
708 text is as expected.
709
710 As before, to make our test case a stand-alone executable,
711 the following two lines are needed:
712
713 \snippet examples/qtestlib/tutorial4/testgui.cpp 3
714
715 The QTEST_MAIN() macro expands to a simple main() method that
716 runs all the test functions, and since both the declaration and
717 the implementation of our test class are in a .cpp file, we also
718 need to include the generated moc file to make Qt's introspection
719 work.
720*/
721
722/*!
723 \example qtestlib/tutorial5
724
725 \previouspage {Chapter 4: Replaying GUI Events}{Chapter 4}
726 \contentspage {QTestLib Tutorial}{Contents}
727
728 \title Chapter 5: Writing a Benchmark
729
730 In this final chapter we will demonstrate how to write benchmarks
731 using QTestLib.
732
733 \section1 Writing a Benchmark
734 To create a benchmark we extend a test function with a QBENCHMARK macro.
735 A benchmark test function will then typically consist of setup code and
736 a QBENCHMARK macro that contains the code to be measured. This test
737 function benchmarks QString::localeAwareCompare().
738
739 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 0
740
741 Setup can be done at the beginning of the function, the clock is not
742 running at this point. The code inside the QBENCHMARK macro will be
743 measured, and possibly repeated several times in order to get an
744 accurate measurement.
745
746 Several \l {testlib-benchmarking-measurement}{back-ends} are available
747 and can be selected on the command line.
748
749 \section1 Data Functions
750
751 Data functions are useful for creating benchmarks that compare
752 multiple data inputs, for example locale aware compare against standard
753 compare.
754
755 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 1
756
757 The test function then uses the data to determine what to benchmark.
758
759 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 2
760
761 The "if(useLocaleCompare)" switch is placed outside the QBENCHMARK
762 macro to avoid measuring its overhead. Each benchmark test function
763 can have one active QBENCHMARK macro.
764
765 \section1 External Tools
766
767 Tools for handling and visualizing test data are available as part of
768 the \l{qtestlib-tools} project on the Qt Labs Web site. These include
769 a tool for comparing performance data obtained from test runs and a
770 utility to generate Web-based graphs of performance data.
771
772 See the \l{qtestlib-tools Announcement} for more information on these
773 tools and a simple graphing example.
774
775*/
776
777
778
Note: See TracBrowser for help on using the repository browser.