source: trunk/doc/src/qtestlib.qdoc@ 109

Last change on this file since 109 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 28.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page qtestlib-manual.html
44 \title QTestLib Manual
45 \ingroup architecture
46 \brief An overview of Qt's unit testing framework.
47
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 \ingroup howto
387
388 This tutorial gives a short introduction to how to use some of the
389 features of the QTestLib framework. It is divided into four
390 chapters:
391
392 \list 1
393 \o \l {Chapter 1: Writing a Unit Test}{Writing a Unit Test}
394 \o \l {Chapter 2: Data Driven Testing}{Data Driven Testing}
395 \o \l {Chapter 3: Simulating GUI Events}{Simulating GUI Events}
396 \o \l {Chapter 4: Replaying GUI Events}{Replaying GUI Events}
397 \o \l {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
398 \endlist
399
400*/
401
402
403/*!
404 \example qtestlib/tutorial1
405
406 \contentspage {QTestLib Tutorial}{Contents}
407 \nextpage {Chapter 2: Data Driven Testing}{Chapter 2}
408
409 \title Chapter 1: Writing a Unit Test
410
411 In this first chapter we will see how to write a simple unit test
412 for a class, and how to execute it.
413
414 \section1 Writing a Test
415
416 Let's assume you want to test the behavior of our QString class.
417 First, you need a class that contains your test functions. This class
418 has to inherit from QObject:
419
420 \snippet examples/qtestlib/tutorial1/testqstring.cpp 0
421
422 Note that you need to include the QTest header, and that the
423 test functions have to be declared as private slots so the
424 test framework finds and executes it.
425
426 Then you need to implement the test function itself. The
427 implementation could look like this:
428
429 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 8
430
431 The \l QVERIFY() macro evaluates the expression passed as its
432 argument. If the expression evaluates to true, the execution of
433 the test function continues. Otherwise, a message describing the
434 failure is appended to the test log, and the test function stops
435 executing.
436
437 But if you want a more verbose output to the test log, you should
438 use the \l QCOMPARE() macro instead:
439
440 \snippet examples/qtestlib/tutorial1/testqstring.cpp 1
441
442 If the strings are not equal, the contents of both strings is
443 appended to the test log, making it immediately visible why the
444 comparison failed.
445
446 Finally, to make our test case a stand-alone executable, the
447 following two lines are needed:
448
449 \snippet examples/qtestlib/tutorial1/testqstring.cpp 2
450
451 The \l QTEST_MAIN() macro expands to a simple \c main()
452 method that runs all the test functions. Note that if both the
453 declaration and the implementation of our test class are in a \c
454 .cpp file, we also need to include the generated moc file to make
455 Qt's introspection work.
456
457 \section1 Executing a Test
458
459 Now that we finished writing our test, we want to execute
460 it. Assuming that our test was saved as \c testqstring.cpp in an
461 empty directory: we build the test using qmake to create a project
462 and generate a makefile.
463
464 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 9
465
466 \bold {Note:}If you're using windows, replace \c make with \c
467 nmake or whatever build tool you use.
468
469 Running the resulting executable should give you the following
470 output:
471
472 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 10
473
474 Congratulations! You just wrote and executed your first unit test
475 using the QTestLib framework.
476*/
477
478/*!
479 \example qtestlib/tutorial2
480
481 \previouspage {Chapter 1: Writing a Unit Test}{Chapter 1}
482 \contentspage {QTestLib Tutorial}{Contents}
483 \nextpage {Chapter 3: Simulating Gui Events}{Chapter 3}
484
485 \title Chapter 2: Data Driven Testing
486
487 In this chapter we will demonstrate how to execute a test
488 multiple times with different test data.
489
490 So far, we have hard coded the data we wanted to test into our
491 test function. If we add more test data, the function might look like
492 this:
493
494 \snippet doc/src/snippets/code/doc_src_qtestlib.qdoc 11
495
496 To prevent that the function ends up being cluttered by repetitive
497 code, QTestLib supports adding test data to a test function. All
498 we need is to add another private slot to our test class:
499
500 \snippet examples/qtestlib/tutorial2/testqstring.cpp 0
501
502 \section1 Writing the Data Function
503
504 A test function's associated data function carries the same name,
505 appended by \c{_data}. Our data function looks like this:
506
507 \snippet examples/qtestlib/tutorial2/testqstring.cpp 1
508
509 First, we define the two elements of our test table using the \l
510 QTest::addColumn() function: A test string, and the
511 expected result of applying the QString::toUpper() function to
512 that string.
513
514 Then we add some data to the table using the \l
515 QTest::newRow() function. Each set of data will become a
516 separate row in the test table.
517
518 \l QTest::newRow() takes one argument: A name that will be
519 associated with the data set. If the test fails, the name will be
520 used in the test log, referencing the failed data. Then we
521 stream the data set into the new table row: First an arbitrary
522 string, and then the expected result of applying the
523 QString::toUpper() function to that string.
524
525 You can think of the test data as a two-dimensional table. In
526 our case, it has two columns called \c string and \c result and
527 three rows. In addition a name as well as an index is associated
528 with each row:
529
530 \table
531 \header
532 \o index
533 \o name
534 \o string
535 \o result
536 \row
537 \o 0
538 \o all lower
539 \o "hello"
540 \o HELLO
541 \row
542 \o 1
543 \o mixed
544 \o "Hello"
545 \o HELLO
546 \row
547 \o 2
548 \o all upper
549 \o "HELLO"
550 \o HELLO
551 \endtable
552
553 \section1 Rewriting the Test Function
554
555 Our test function can now be rewritten:
556
557 \snippet examples/qtestlib/tutorial2/testqstring.cpp 2
558
559 The TestQString::toUpper() function will be executed three times,
560 once for each entry in the test table that we created in the
561 associated TestQString::toUpper_data() function.
562
563 First, we fetch the two elements of the data set using the \l
564 QFETCH() macro. \l QFETCH() takes two arguments: The data type of
565 the element and the element name. Then we perform the test using
566 the \l QCOMPARE() macro.
567
568 This approach makes it very easy to add new data to the test
569 without modifying the test itself.
570
571 And again, to make our test case a stand-alone executable,
572 the following two lines are needed:
573
574 \snippet examples/qtestlib/tutorial2/testqstring.cpp 3
575
576 As before, the QTEST_MAIN() macro expands to a simple main()
577 method that runs all the test functions, and since both the
578 declaration and the implementation of our test class are in a .cpp
579 file, we also need to include the generated moc file to make Qt's
580 introspection work.
581*/
582
583/*!
584 \example qtestlib/tutorial3
585
586 \previouspage {Chapter 2 Data Driven Testing}{Chapter 2}
587 \contentspage {QTestLib Tutorial}{Contents}
588 \nextpage {Chapter 4: Replaying GUI Events}{Chapter 4}
589
590 \title Chapter 3: Simulating GUI Events
591
592 QTestLib features some mechanisms to test graphical user
593 interfaces. Instead of simulating native window system events,
594 QTestLib sends internal Qt events. That means there are no
595 side-effects on the machine the tests are running on.
596
597 In this chapter we will se how to write a simple GUI test.
598
599 \section1 Writing a GUI test
600
601 This time, let's assume you want to test the behavior of our
602 QLineEdit class. As before, you will need a class that contains
603 your test function:
604
605 \snippet examples/qtestlib/tutorial3/testgui.cpp 0
606
607 The only difference is that you need to include the QtGui class
608 definitions in addition to the QTest namespace.
609
610 \snippet examples/qtestlib/tutorial3/testgui.cpp 1
611
612 In the implementation of the test function we first create a
613 QLineEdit. Then we simulate writing "hello world" in the line edit
614 using the \l QTest::keyClicks() function.
615
616 \note The widget must also be shown in order to correctly test keyboard
617 shortcuts.
618
619 QTest::keyClicks() simulates clicking a sequence of keys on a
620 widget. Optionally, a keyboard modifier can be specified as well
621 as a delay (in milliseconds) of the test after each key click. In
622 a similar way, you can use the QTest::keyClick(),
623 QTest::keyPress(), QTest::keyRelease(), QTest::mouseClick(),
624 QTest::mouseDClick(), QTest::mouseMove(), QTest::mousePress()
625 and QTest::mouseRelease() functions to simulate the associated
626 GUI events.
627
628 Finally, we use the \l QCOMPARE() macro to check if the line edit's
629 text is as expected.
630
631 As before, to make our test case a stand-alone executable, the
632 following two lines are needed:
633
634 \snippet examples/qtestlib/tutorial3/testgui.cpp 2
635
636 The QTEST_MAIN() macro expands to a simple main() method that
637 runs all the test functions, and since both the declaration and
638 the implementation of our test class are in a .cpp file, we also
639 need to include the generated moc file to make Qt's introspection
640 work.
641*/
642
643/*!
644 \example qtestlib/tutorial4
645
646 \previouspage {Chapter 3: Simulating GUI Event}{Chapter 3}
647 \contentspage {QTestLib Tutorial}{Contents}
648 \nextpage {Chapter 5: Writing a Benchmark}{Chapter 5}
649
650 \title Chapter 4: Replaying GUI Events
651
652 In this chapter, we will show how to simulate a GUI event,
653 and how to store a series of GUI events as well as replay them on
654 a widget.
655
656 The approach to storing a series of events and replay them, is
657 quite similar to the approach explained in \l {Chapter 2:
658 Data Driven Testing}{chapter 2}; all you need is to add a data
659 function to your test class:
660
661 \snippet examples/qtestlib/tutorial4/testgui.cpp 0
662
663 \section1 Writing the Data Function
664
665 As before, a test function's associated data function carries the
666 same name, appended by \c{_data}.
667
668 \snippet examples/qtestlib/tutorial4/testgui.cpp 1
669
670 First, we define the elements of the table using the
671 QTest::addColumn() function: A list of GUI events, and the
672 expected result of applying the list of events on a QWidget. Note
673 that the type of the first element is \l QTestEventList.
674
675 A QTestEventList can be populated with GUI events that can be
676 stored as test data for later usage, or be replayed on any
677 QWidget.
678
679 In our current data function, we create two \l
680 {QTestEventList}s. The first list consists of a single click to
681 the 'a' key. We add the event to the list using the
682 QTestEventList::addKeyClick() function. Then we use the
683 QTest::newRow() function to give the data set a name, and
684 stream the event list and the expected result into the table.
685
686 The second list consists of two key clicks: an 'a' with a
687 following 'backspace'. Again we use the
688 QTestEventList::addKeyClick() to add the events to the list, and
689 QTest::newRow() to put the event list and the expected
690 result into the table with an associated name.
691
692 \section1 Rewriting the Test Function
693
694 Our test can now be rewritten:
695
696 \snippet examples/qtestlib/tutorial4/testgui.cpp 2
697
698 The TestGui::testGui() function will be executed two times,
699 once for each entry in the test data that we created in the
700 associated TestGui::testGui_data() function.
701
702 First, we fetch the two elements of the data set using the \l
703 QFETCH() macro. \l QFETCH() takes two arguments: The data type of
704 the element and the element name. Then we create a QLineEdit, and
705 apply the list of events on that widget using the
706 QTestEventList::simulate() function.
707
708 Finally, we use the QCOMPARE() macro to check if the line edit's
709 text is as expected.
710
711 As before, to make our test case a stand-alone executable,
712 the following two lines are needed:
713
714 \snippet examples/qtestlib/tutorial4/testgui.cpp 3
715
716 The QTEST_MAIN() macro expands to a simple main() method that
717 runs all the test functions, and since both the declaration and
718 the implementation of our test class are in a .cpp file, we also
719 need to include the generated moc file to make Qt's introspection
720 work.
721*/
722
723/*!
724 \example qtestlib/tutorial5
725
726 \previouspage {Chapter 4: Replaying GUI Events}{Chapter 4}
727 \contentspage {QTestLib Tutorial}{Contents}
728
729 \title Chapter 5: Writing a Benchmark
730
731 In this final chapter we will demonstrate how to write benchmarks
732 using QTestLib.
733
734 \section1 Writing a Benchmark
735 To create a benchmark we extend a test function with a QBENCHMARK macro.
736 A benchmark test function will then typically consist of setup code and
737 a QBENCHMARK macro that contains the code to be measured. This test
738 function benchmarks QString::localeAwareCompare().
739
740 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 0
741
742 Setup can be done at the beginning of the function, the clock is not
743 running at this point. The code inside the QBENCHMARK macro will be
744 measured, and possibly repeated several times in order to get an
745 accurate measurement.
746
747 Several \l {testlib-benchmarking-measurement}{back-ends} are available
748 and can be selected on the command line.
749
750 \section1 Data Functions
751
752 Data functions are useful for creating benchmarks that compare
753 multiple data inputs, for example locale aware compare against standard
754 compare.
755
756 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 1
757
758 The test function then uses the data to determine what to benchmark.
759
760 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 2
761
762 The "if(useLocaleCompare)" switch is placed outside the QBENCHMARK
763 macro to avoid measuring its overhead. Each benchmark test function
764 can have one active QBENCHMARK macro.
765
766 \section1 External Tools
767
768 Tools for handling and visualizing test data are available as part of
769 the \l{qtestlib-tools} project on the Qt Labs Web site. These include
770 a tool for comparing performance data obtained from test runs and a
771 utility to generate Web-based graphs of performance data.
772
773 See the \l{qtestlib-tools Announcement} for more information on these
774 tools and a simple graphing example.
775
776*/
777
778
779
Note: See TracBrowser for help on using the repository browser.