Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qtestcase.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \namespace QTest
6 \inmodule QtTest
7
8 \brief The QTest namespace contains all the functions and
9 declarations that are related to Qt Test.
10
11 See the \l{Qt Test Overview} for information about how to write unit tests.
12*/
13
14/*! \macro QVERIFY(condition)
15
16 \relates QTest
17
18 The QVERIFY() macro checks whether the \a condition is true or not. If it is
19 true, execution continues. If not, a failure is recorded in the test log
20 and the test won't be executed further.
21
22 You can use \l QVERIFY2() when it is practical and valuable to put additional
23 information into the test failure report.
24
25//! [macro-usage-limitation]
26 \note This macro can only be used in a test function that is invoked
27 by the test framework.
28//! [macro-usage-limitation]
29
30 For example, the following code shows this macro being used to verify that a
31 \l QSignalSpy object is valid:
32
33 \snippet code/src_qtestlib_qtestcase_snippet.cpp 0
34
35 For more information about the failure, use \c QCOMPARE(x, y) instead of
36 \c QVERIFY(x == y), because it reports both the expected and actual value
37 when the comparison fails.
38
39 \sa QCOMPARE(), QTRY_VERIFY(), QSignalSpy, QEXPECT_FAIL(), QCOMPARE_EQ(),
40 QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
41*/
42
43/*! \macro QVERIFY2(condition, message)
44
45 \relates QTest
46
47 The QVERIFY2() macro behaves exactly like QVERIFY(), except that it reports
48 a \a message when \a condition is false. The \a message is a plain C string.
49
50 The message can also be obtained from a function call that produces a plain
51 C string, such as qPrintable() applied to a QString, which may be built in
52 any of its usual ways, including applying \c {.args()} to format some data.
53
54 Example:
55 \snippet code/src_qtestlib_qtestcase.cpp 1
56
57 For example, if you have a file object and you are testing its \c open()
58 function, you might write a test with a statement like:
59
60 \snippet code/src_qtestlib_qtestcase.cpp 32
61
62 If this test fails, it will give no clue as to why the file failed to open:
63
64 \c {FAIL! : tst_QFile::open_write() 'opened' returned FALSE. ()}
65
66 If there is a more informative error message you could construct from the
67 values being tested, you can use \c QVERIFY2() to pass that message along
68 with your test condition, to provide a more informative message on failure:
69
70 \snippet code/src_qtestlib_qtestcase.cpp 33
71
72 If this branch is being tested in the Qt CI system, the above detailed
73 failure message will be inserted into the summary posted to the code-review
74 system:
75
76 \c {FAIL! : tst_QFile::open_write() 'opened' returned FALSE.
77 (open /tmp/qt.a3B42Cd: No space left on device)}
78
79 \sa QVERIFY(), QCOMPARE(), QEXPECT_FAIL(), QCOMPARE_EQ(), QCOMPARE_NE(),
80 QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
81*/
82
83/*! \macro QCOMPARE(actual, expected)
84
85 \relates QTest
86
87 The QCOMPARE() macro compares an \a actual value to an \a expected value
88 using the equality operator. If \a actual and \a expected match, execution
89 continues. If not, a failure is recorded in the test log and the test
90 function returns without attempting any later checks.
91
92 Always respect QCOMPARE() parameter semantics. The first parameter passed to
93 it should always be the actual value produced by the code-under-test, while
94 the second parameter should always be the expected value. When the values
95 don't match, QCOMPARE() prints them with the labels \e Actual and \e
96 Expected. If the parameter order is swapped, debugging a failing test can be
97 confusing and tests expecting zero may fail due to rounding errors.
98
99 QCOMPARE() tries to output the contents of the values if the comparison fails,
100 so it is visible from the test log why the comparison failed.
101
102 Example:
103 \snippet code/src_qtestlib_qtestcase.cpp 2
104
105 When comparing floating-point types (\c float, \c double, and \c qfloat16),
106 \l {qFuzzyCompare()} is used for finite values. If \l {<QtNumeric>::}{qFuzzyIsNull()}
107 is true for both values, they are also considered equal. Infinities
108 match if they have the same sign, and any NaN as actual value matches
109 with any NaN as expected value (even though NaN != NaN, even when
110 they're identical).
111
112 When comparing QList, arrays and initializer lists of the value type
113 can be passed as expected value:
114 \snippet code/src_qtestlib_qtestcase.cpp 34
115
116 Note that using initializer lists requires defining a helper macro
117 to prevent the preprocessor from interpreting the commas as macro argument
118 delimiters:
119 \snippet code/src_qtestlib_qtestcase.cpp 35
120
121 \include qtestcase.qdoc macro-usage-limitation
122
123//! [to-string-overload-desc]
124 For your own classes, you can overload \l QTest::toString() to format values
125 for output into the test log.
126//! [to-string-overload-desc]
127
128 Example:
129 \snippet code/src_qtestlib_qtestcase_snippet.cpp 34
130
131 The return from \c toString() must be a \c {new char []}. That is, it shall
132 be released with \c delete[] (rather than \c free() or plain \c delete) once
133 the calling code is done with it.
134
135 \sa QVERIFY(), QTRY_COMPARE(), QTest::toString(), QEXPECT_FAIL(),
136 QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(),
137 QCOMPARE_GT(), QCOMPARE_GE()
138*/
139
140/*! \macro QCOMPARE_EQ(computed, baseline)
141 \since 6.4
142
143 \relates QTest
144
145 The QCOMPARE_EQ() macro checks that \a computed is equal to \a baseline using
146 the equality operator. If that is true, execution continues. If not, a
147 failure is recorded in the test log and the test function returns without
148 attempting any later checks.
149
150 It is generally similar to calling \c {QVERIFY(computed == baseline);}
151 but prints a formatted error message reporting \a computed and \a baseline argument
152 expressions and values in case of failure.
153
154 \include qtestcase.qdoc macro-usage-limitation
155
156 \include qtestcase.qdoc to-string-overload-desc
157
158 \note Unlike QCOMPARE(), this macro does not provide overloads for custom
159 types and pointers. So passing e.g. two \c {const char *} values as
160 parameters will compare \e pointers, while QCOMPARE() does a comparison of
161 C-style strings.
162
163 \sa QCOMPARE(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(),
164 QCOMPARE_GE()
165*/
166
167/*! \macro QCOMPARE_NE(computed, baseline)
168 \since 6.4
169
170 \relates QTest
171
172 The QCOMPARE_NE() macro checks that \a computed is not equal to \a baseline using
173 the inequality operator. If that is true, execution continues. If not, a
174 failure is recorded in the test log and the test function returns without
175 attempting any later checks.
176
177 It is generally similar to calling \c {QVERIFY(computed != baseline);}
178 but prints a formatted error message reporting \a computed and \a baseline argument
179 expressions and values in case of failure.
180
181 \include qtestcase.qdoc macro-usage-limitation
182
183 \include qtestcase.qdoc to-string-overload-desc
184
185 \sa QCOMPARE_EQ(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
186*/
187
188/*! \macro QCOMPARE_LT(computed, baseline)
189 \since 6.4
190
191 \relates QTest
192
193 The QCOMPARE_LT() macro checks that \a computed is less than \a baseline using the
194 less-than operator. If that is true, execution continues. If not, a failure
195 is recorded in the test log and the test function returns without attempting
196 any later checks.
197
198 It is generally similar to calling \c {QVERIFY(computed < baseline);}
199 but prints a formatted error message reporting \a computed and \a baseline argument
200 expressions and values in case of failure.
201
202 \include qtestcase.qdoc macro-usage-limitation
203
204 \include qtestcase.qdoc to-string-overload-desc
205
206 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
207*/
208
209/*! \macro QCOMPARE_LE(computed, baseline)
210 \since 6.4
211
212 \relates QTest
213
214 The QCOMPARE_LE() macro checks that \a computed is at most \a baseline using the
215 less-than-or-equal-to operator. If that is true, execution continues. If
216 not, a failure is recorded in the test log and the test function returns
217 without attempting any later checks.
218
219 It is generally similar to calling \c {QVERIFY(computed <= baseline);}
220 but prints a formatted error message reporting \a computed and \a baseline argument
221 expressions and values in case of failure.
222
223 \include qtestcase.qdoc macro-usage-limitation
224
225 \include qtestcase.qdoc to-string-overload-desc
226
227 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_GT(), QCOMPARE_GE()
228*/
229
230/*! \macro QCOMPARE_GT(computed, baseline)
231 \since 6.4
232
233 \relates QTest
234
235 The QCOMPARE_GT() macro checks that \a computed is greater than \a baseline using
236 the greater-than operator. If that is true, execution continues. If not, a
237 failure is recorded in the test log and the test function returns without
238 attempting any later checks.
239
240 It is generally similar to calling \c {QVERIFY(computed > baseline);}
241 but prints a formatted error message reporting \a computed and \a baseline argument
242 expressions and values in case of failure.
243
244 \include qtestcase.qdoc macro-usage-limitation
245
246 \include qtestcase.qdoc to-string-overload-desc
247
248 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GE()
249*/
250
251/*! \macro QCOMPARE_GE(computed, baseline)
252 \since 6.4
253
254 \relates QTest
255
256 The QCOMPARE_GE() macro checks that \a computed is at least \a baseline using the
257 greater-than-or-equal-to operator. If that is true, execution continues. If
258 not, a failure is recorded in the test log and the test function returns
259 without attempting any later checks.
260
261 It is generally similar to calling \c {QVERIFY(computed >= baseline);}
262 but prints a formatted error message reporting \a computed and \a baseline argument
263 expressions and values in case of failure.
264
265 \include qtestcase.qdoc macro-usage-limitation
266
267 \include qtestcase.qdoc to-string-overload-desc
268
269 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT()
270*/
271
272/*! \macro QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)
273 \since 5.3
274
275 \relates QTest
276 \deprecated [6.3] Use \c{QVERIFY_THROWS_EXCEPTION(exceptiontype, expression)} instead.
277*/
278
279/*!
280 \macro QVERIFY_THROWS_EXCEPTION(exceptiontype, ...)
281 \relates QTest
282 \since 6.3
283
284 The QVERIFY_THROWS_EXCEPTION macro executes the expression given in the variadic
285 argument and expects to catch an exception thrown from the expression.
286
287 There are several possible outcomes:
288
289 \list
290 \li If the expression throws an exception that is either the same as
291 \a exceptiontype or derived from \a exceptiontype, then execution will continue.
292
293 \li Otherwise, if the expression throws no exception, or the
294 exception thrown derives from \c{std::exception}, then a failure
295 will be recorded in the test log and the macro returns early
296 (from enclosing function).
297
298 \li If the thrown exception derives neither from \c{std::exception} nor from
299 \a exceptiontype, a failure will be recorded in the test log, and the exception is
300 re-thrown. This avoids problems with e.g. pthread cancellation exceptions.
301 \endlist
302
303 The macro uses variadic arguments so the expression can contain commas that the
304 preprocessor considers argument separators, e.g. as in
305 \code
306 QVERIFY_THROWS_EXCEPTION(std::bad_alloc,
307 // macro arguments: ^ exceptiontype
308 std::vector<std::pair<int, long>>{42'000'000'000, {42, 42L}});
309 // macro arguments: \---------- 1 ----------/ \-------- 2 --------/ \3/ \ 4 /
310 // \----------------------- expression -----------------------/
311 \endcode
312
313 \note This macro can only be used in a test function that is invoked
314 by the test framework.
315*/
316
317/*!
318 \macro QVERIFY_THROWS_NO_EXCEPTION(...)
319 \since 6.3
320
321 \relates QTest
322
323 The QVERIFY_THROWS_NO_EXCEPTION macro executes the expression given in its
324 variadic argument and tries to catch any exception thrown from the expression.
325
326 There are several different outcomes:
327
328 \list
329 \li If the expression does not throw an exception, then execution will continue.
330
331 \li Otherwise, if an exception derived from \c{std::exception} is caught, a failure
332 will be recorded in the test log and the macro returns early (implicit return from
333 enclosing function).
334
335 \li If an exception not derived from \c{std::exception} is caught, a failure will be
336 recorded in the test log and the exception will be re-thrown. This avoids problems
337 with e.g. pthread cancellation exceptions.
338 \endlist
339
340 The macro uses variadic arguments so the expression can contain commas that the
341 preprocessor considers argument separators, e.g. as in
342 \code
343 QVERIFY_THROWS_NO_EXCEPTION(std::pair<int, long>{42, 42L});
344 // macro arguments: \---- 1 ----/ \-- 2 -/ \3 /
345 \endcode
346
347 \note This macro can only be used in a test function that is invoked
348 by the test framework.
349*/
350
351/*! \macro QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
352 \since 5.0
353
354 \relates QTest
355
356 The QTRY_VERIFY_WITH_TIMEOUT() macro is similar to QVERIFY(), but checks the \a condition
357 repeatedly, until either the condition becomes true or the \a timeout (in milliseconds) is
358 reached. Between each evaluation, events will be processed. If the timeout
359 is reached, a failure is recorded in the test log and the test won't be
360 executed further.
361
362 \note This macro can only be used in a test function that is invoked
363 by the test framework.
364
365 \sa QTRY_VERIFY(), QTRY_VERIFY2_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
366 QEXPECT_FAIL()
367*/
368
369
370/*! \macro QTRY_VERIFY(condition)
371 \since 5.0
372
373 \relates QTest
374
375 Checks the \a condition by invoking QTRY_VERIFY_WITH_TIMEOUT() with a timeout of five seconds.
376
377 \note This macro can only be used in a test function that is invoked
378 by the test framework.
379
380 \sa QTRY_VERIFY_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
381 QEXPECT_FAIL()
382*/
383
384/*! \macro QTRY_VERIFY2_WITH_TIMEOUT(condition, message, timeout)
385 \since 5.6
386
387 \relates QTest
388
389 The QTRY_VERIFY2_WITH_TIMEOUT macro is similar to QTRY_VERIFY_WITH_TIMEOUT()
390 except that it outputs a verbose \a message when \a condition is still false
391 after the specified \a timeout (in milliseconds). The \a message is a plain C string.
392
393 Example:
394 \code
395 QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10000);
396 \endcode
397
398 \note This macro can only be used in a test function that is invoked
399 by the test framework.
400
401 \sa QTRY_VERIFY(), QTRY_VERIFY_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
402 QEXPECT_FAIL()
403*/
404
405/*! \macro QTRY_VERIFY2(condition, message)
406 \since 5.6
407
408 \relates QTest
409
410 Checks the \a condition by invoking QTRY_VERIFY2_WITH_TIMEOUT() with a timeout
411 of five seconds. If \a condition is then still false, \a message is output.
412 The \a message is a plain C string.
413
414 Example:
415 \code
416 QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData());
417 \endcode
418
419 \note This macro can only be used in a test function that is invoked
420 by the test framework.
421
422 \sa QTRY_VERIFY2_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
423 QEXPECT_FAIL()
424*/
425
426/*! \macro QTRY_COMPARE_WITH_TIMEOUT(actual, expected, timeout)
427 \since 5.0
428
429 \relates QTest
430
431 The QTRY_COMPARE_WITH_TIMEOUT() macro is similar to QCOMPARE(), but performs the comparison
432 of the \a actual and \a expected values repeatedly, until either the two values
433 are equal or the \a timeout (in milliseconds) is reached. Between each comparison, events
434 will be processed. If the timeout is reached, a failure is recorded in the
435 test log and the test won't be executed further.
436
437 \note This macro can only be used in a test function that is invoked
438 by the test framework.
439
440 \sa QTRY_COMPARE(), QCOMPARE(), QVERIFY(), QTRY_VERIFY(), QEXPECT_FAIL()
441*/
442
443/*! \macro QTRY_COMPARE(actual, expected)
444 \since 5.0
445
446 \relates QTest
447
448 Performs a comparison of the \a actual and \a expected values by
449 invoking QTRY_COMPARE_WITH_TIMEOUT() with a timeout of five seconds.
450
451 \note This macro can only be used in a test function that is invoked
452 by the test framework.
453
454 \sa QTRY_COMPARE_WITH_TIMEOUT(), QCOMPARE(), QVERIFY(), QTRY_VERIFY(),
455 QEXPECT_FAIL()
456*/
457
458/*! \macro QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout)
459 \since 6.4
460 \relates QTest
461
462 This macro is similar to QCOMPARE_EQ(), but performs the comparison of the
463 \a computed and \a baseline values repeatedly, until either the comparison returns
464 \c true or the \a timeout (in milliseconds) is reached. Between each
465 comparison, events will be processed. If the timeout is reached, a failure
466 is recorded in the test log and the test won't be executed further.
467
468 \include qtestcase.qdoc macro-usage-limitation
469
470 \sa QCOMPARE_EQ(), QTRY_COMPARE_EQ()
471*/
472
473/*! \macro QTRY_COMPARE_EQ(computed, baseline)
474 \since 6.4
475 \relates QTest
476
477 Performs comparison of \a computed and \a baseline values by invoking
478 QTRY_COMPARE_EQ_WITH_TIMEOUT with a timeout of five seconds.
479
480 \include qtestcase.qdoc macro-usage-limitation
481
482 \sa QCOMPARE_EQ(), QTRY_COMPARE_EQ_WITH_TIMEOUT()
483*/
484
485/*! \macro QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout)
486 \since 6.4
487 \relates QTest
488
489 This macro is similar to QCOMPARE_NE(), but performs the comparison of the
490 \a computed and \a baseline values repeatedly, until either the comparison returns
491 \c true or the \a timeout (in milliseconds) is reached. Between each
492 comparison, events will be processed. If the timeout is reached, a failure
493 is recorded in the test log and the test won't be executed further.
494
495 \include qtestcase.qdoc macro-usage-limitation
496
497 \sa QCOMPARE_NE(), QTRY_COMPARE_NE()
498*/
499
500/*! \macro QTRY_COMPARE_NE(computed, baseline)
501 \since 6.4
502 \relates QTest
503
504 Performs comparison of \a computed and \a baseline values by invoking
505 QTRY_COMPARE_NE_WITH_TIMEOUT with a timeout of five seconds.
506
507 \include qtestcase.qdoc macro-usage-limitation
508
509 \sa QCOMPARE_NE(), QTRY_COMPARE_NE_WITH_TIMEOUT()
510*/
511
512/*! \macro QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout)
513 \since 6.4
514 \relates QTest
515
516 This macro is similar to QCOMPARE_LT(), but performs the comparison of the
517 \a computed and \a baseline values repeatedly, until either the comparison returns
518 \c true or the \a timeout (in milliseconds) is reached. Between each
519 comparison, events will be processed. If the timeout is reached, a failure
520 is recorded in the test log and the test won't be executed further.
521
522 \include qtestcase.qdoc macro-usage-limitation
523
524 \sa QCOMPARE_LT(), QTRY_COMPARE_LT()
525*/
526
527/*! \macro QTRY_COMPARE_LT(computed, baseline)
528 \since 6.4
529 \relates QTest
530
531 Performs comparison of \a computed and \a baseline values by invoking
532 QTRY_COMPARE_LT_WITH_TIMEOUT with a timeout of five seconds.
533
534 \include qtestcase.qdoc macro-usage-limitation
535
536 \sa QCOMPARE_LT(), QTRY_COMPARE_LT_WITH_TIMEOUT()
537*/
538
539/*! \macro QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout)
540 \since 6.4
541 \relates QTest
542
543 This macro is similar to QCOMPARE_LE(), but performs the comparison of the
544 \a computed and \a baseline values repeatedly, until either the comparison returns
545 \c true or the \a timeout (in milliseconds) is reached. Between each
546 comparison, events will be processed. If the timeout is reached, a failure
547 is recorded in the test log and the test won't be executed further.
548
549 \include qtestcase.qdoc macro-usage-limitation
550
551 \sa QCOMPARE_LE(), QTRY_COMPARE_LE()
552*/
553
554/*! \macro QTRY_COMPARE_LE(computed, baseline)
555 \since 6.4
556 \relates QTest
557
558 Performs comparison of \a computed and \a baseline values by invoking
559 QTRY_COMPARE_LE_WITH_TIMEOUT with a timeout of five seconds.
560
561 \include qtestcase.qdoc macro-usage-limitation
562
563 \sa QCOMPARE_LE(), QTRY_COMPARE_LE_WITH_TIMEOUT()
564*/
565
566/*! \macro QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout)
567 \since 6.4
568 \relates QTest
569
570 This macro is similar to QCOMPARE_GT(), but performs the comparison of the
571 \a computed and \a baseline values repeatedly, until either the comparison returns
572 \c true or the \a timeout (in milliseconds) is reached. Between each
573 comparison, events will be processed. If the timeout is reached, a failure
574 is recorded in the test log and the test won't be executed further.
575
576 \include qtestcase.qdoc macro-usage-limitation
577
578 \sa QCOMPARE_GT(), QTRY_COMPARE_GT()
579*/
580
581/*! \macro QTRY_COMPARE_GT(computed, baseline)
582 \since 6.4
583 \relates QTest
584
585 Performs comparison of \a computed and \a baseline values by invoking
586 QTRY_COMPARE_GT_WITH_TIMEOUT with a timeout of five seconds.
587
588 \include qtestcase.qdoc macro-usage-limitation
589
590 \sa QCOMPARE_GT(), QTRY_COMPARE_GT_WITH_TIMEOUT()
591*/
592
593/*! \macro QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout)
594 \since 6.4
595 \relates QTest
596
597 This macro is similar to QCOMPARE_GE(), but performs the comparison of the
598 \a computed and \a baseline values repeatedly, until either the comparison returns
599 \c true or the \a timeout (in milliseconds) is reached. Between each
600 comparison, events will be processed. If the timeout is reached, a failure
601 is recorded in the test log and the test won't be executed further.
602
603 \include qtestcase.qdoc macro-usage-limitation
604
605 \sa QCOMPARE_GE(), QTRY_COMPARE_GE()
606*/
607
608/*! \macro QTRY_COMPARE_GE(computed, baseline)
609 \since 6.4
610 \relates QTest
611
612 Performs comparison of \a computed and \a baseline values by invoking
613 QTRY_COMPARE_GE_WITH_TIMEOUT with a timeout of five seconds.
614
615 \include qtestcase.qdoc macro-usage-limitation
616
617 \sa QCOMPARE_GE(), QTRY_COMPARE_GE_WITH_TIMEOUT()
618*/
619
620/*! \macro QFETCH(type, name)
621
622 \relates QTest
623
624 The fetch macro creates a local variable named \a name with the type \a type
625 on the stack. The \a name and \a type must match a column from the test's
626 data table. This is asserted and the test will abort if the assertion fails.
627
628 Assuming a test has the following data:
629
630 \snippet code/src_qtestlib_qtestcase.cpp 3
631
632 The test data has two elements, a QString called \c aString and an integer
633 called \c expected. To fetch these values in the actual test:
634
635 \snippet code/src_qtestlib_qtestcase.cpp 4
636
637 \c aString and \c expected are variables on the stack that are initialized with
638 the current test data.
639
640 \note This macro can only be used in a test function that is invoked
641 by the test framework. The test function must have a _data function.
642*/
643
644/*! \macro QFETCH_GLOBAL(type, name)
645
646 \relates QTest
647
648 This macro fetches a variable named \a name with the type \a type from
649 a row in the global data table. The \a name and \a type must match a
650 column in the global data table. This is asserted and the test will abort
651 if the assertion fails.
652
653 Assuming a test has the following data:
654
655 \snippet code/src_qtestlib_qtestcase_snippet.cpp 30
656
657 The test's own data is a single number per row. In this case,
658 \c initTestCase_data() also supplies a locale per row. Therefore,
659 this test will be run with every combination of locale from the
660 latter and number from the former. Thus, with four rows in the
661 global table and three in the local, the test function is run for
662 12 distinct test-cases (4 * 3 = 12).
663
664 \snippet code/src_qtestlib_qtestcase_snippet.cpp 31
665
666 The locale is read from the global data table using QFETCH_GLOBAL(),
667 and the number is read from the local data table using QFETCH().
668
669 \note This macro can only be used in test methods of a class with an
670 \c initTestCase_data() method.
671*/
672
673/*! \macro QWARN(message)
674
675 \relates QTest
676 \threadsafe
677 \deprecated Use qWarning() instead.
678
679 Appends \a message as a warning to the test log. This macro can be used anywhere
680 in your tests.
681*/
682
683/*! \macro QFAIL(message)
684
685 \relates QTest
686
687 This macro can be used to force a test failure. The test stops
688 executing and the failure \a message is appended to the test log.
689
690 \note This macro can only be used in a test function that is invoked
691 by the test framework.
692
693 Example:
694
695 \snippet code/src_qtestlib_qtestcase.cpp 5
696*/
697
698/*! \macro QTEST(actual, testElement)
699
700 \relates QTest
701
702 QTEST() is a convenience macro for \l QCOMPARE() that compares
703 the value \a actual with the element \a testElement from the test's data.
704 If there is no such element, the test asserts.
705
706 Apart from that, QTEST() behaves exactly as \l QCOMPARE().
707
708 Instead of writing:
709
710 \snippet code/src_qtestlib_qtestcase.cpp 6
711
712 you can write:
713
714 \snippet code/src_qtestlib_qtestcase.cpp 7
715
716 \sa QCOMPARE()
717*/
718
719/*! \macro QSKIP(description)
720
721 \relates QTest
722
723 If called from a test function, the QSKIP() macro stops execution of the test
724 without adding a failure to the test log. You can use it to skip tests that
725 wouldn't make sense in the current configuration. For example, a test of font
726 rendering may call QSKIP() if the needed fonts are not installed on the test
727 system.
728
729 The text \a description is appended to the test log and should contain an
730 explanation of why the test couldn't be executed.
731
732 If the test is data-driven, each call to QSKIP() in the test function will
733 skip only the current row of test data, so an unconditional call to QSKIP()
734 will produce one skip message in the test log for each row of test data.
735
736 If called from an \c _data function, the QSKIP() macro will stop execution of
737 the \c _data function and will prevent execution of the associated test
738 function. This entirely omits a data-driven test. To omit individual rows,
739 make them conditional by using a simple \c{if (condition) newRow(...) << ...}
740 in the \c _data function, instead of using QSKIP() in the test function.
741
742 If called from \c initTestCase_data(), the QSKIP() macro will skip all test
743 and \c _data functions. If called from \c initTestCase() when there is no
744 \c initTestCase_data(), or when it only sets up one row, QSKIP() will
745 likewise skip the whole test. However, if \c initTestCase_data() contains
746 more than one row, then \c initTestCase() is called (followed by each test
747 and finally the wrap-up) once per row of it. Therefore, a call to QSKIP() in
748 \c initTestCase() will merely skip all test functions for the current row of
749 global data, set up by \c initTestCase_data().
750
751 \note This macro can only be used in a test function or \c _data
752 function that is invoked by the test framework.
753
754 Example:
755 \snippet code/src_qtestlib_qtestcase.cpp 8
756
757 \section2 Skipping Known Bugs
758
759 If a test exposes a known bug that will not be fixed immediately, use the
760 QEXPECT_FAIL() macro to document the failure and reference the bug tracking
761 identifier for the known issue. When the test is run, expected failures will
762 be marked as XFAIL in the test output and will not be counted as failures
763 when setting the test program's return code. If an expected failure does
764 not occur, the XPASS (unexpected pass) will be reported in the test output
765 and will be counted as a test failure.
766
767 For known bugs, QEXPECT_FAIL() is better than QSKIP() because a developer
768 cannot fix the bug without an XPASS result reminding them that the test
769 needs to be updated too. If QSKIP() is used, there is no reminder to revise
770 or re-enable the test, without which subsequent regressions will not be
771 reported.
772
773 \sa QEXPECT_FAIL(), {Select Appropriate Mechanisms to Exclude Tests}
774*/
775
776/*! \macro QEXPECT_FAIL(dataIndex, comment, mode)
777
778 \relates QTest
779
780 The QEXPECT_FAIL() macro marks the next \l QCOMPARE() or \l QVERIFY() as an
781 expected failure. Instead of adding a failure to the test log, an expected
782 failure will be reported.
783
784 If a \l QVERIFY() or \l QCOMPARE() is marked as an expected failure,
785 but passes instead, an unexpected pass (XPASS) is written to the test log.
786
787 The parameter \a dataIndex describes for which entry in the test data the
788 failure is expected. Pass an empty string (\c{""}) if the failure
789 is expected for all entries or if no test data exists.
790
791 \a comment will be appended to the test log for the expected failure.
792
793 \a mode is a \l QTest::TestFailMode and sets whether the test should
794 continue to execute or not. The \a mode is applied regardless of
795 whether the expected test failure occurs.
796
797 \note This macro can only be used in a test function that is invoked
798 by the test framework.
799
800 Example 1:
801 \snippet code/src_qtestlib_qtestcase.cpp 9
802
803 In the example above, an expected fail will be written into the test output
804 if the variable \c i is not 42. If the variable \c i is 42, an unexpected pass
805 is written instead. The QEXPECT_FAIL() has no influence on the second QCOMPARE()
806 statement in the example.
807
808 Example 2:
809 \snippet code/src_qtestlib_qtestcase.cpp 10
810
811 The above testfunction will not continue executing for the test data
812 entry \c{data27} (regardless of the value of \c i).
813
814 \sa QTest::TestFailMode, QVERIFY(), QCOMPARE()
815*/
816
817/*! \macro QFINDTESTDATA(filename)
818 \since 5.0
819
820 \relates QTest
821
822 Returns a QString for the testdata file referred to by \a filename, or an
823 empty QString if the testdata file could not be found.
824
825 This macro allows the test to load data from an external file without
826 hardcoding an absolute filename into the test, or using relative paths
827 which may be error prone.
828
829 The returned path will be the first path from the following list which
830 resolves to an existing file or directory:
831
832 \list
833 \li \a filename relative to QCoreApplication::applicationDirPath()
834 (only if a QCoreApplication or QApplication object has been created).
835 \li \a filename relative to the test's standard install directory
836 (QLibraryInfo::TestsPath with the lowercased testcase name appended).
837 \li \a filename relative to the directory containing the source file from which
838 QFINDTESTDATA is invoked.
839 \endlist
840
841 If the named file/directory does not exist at any of these locations,
842 a warning is printed to the test log.
843
844 For example, in this code:
845 \snippet code/src_qtestlib_qtestcase_snippet.cpp 26
846
847 The testdata file will be resolved as the first existing file from:
848
849 \list
850 \li \c{/home/user/build/myxmlparser/tests/tst_myxmlparser/testxml/simple1.xml}
851 \li \c{/usr/local/Qt-5.0.0/tests/tst_myxmlparser/testxml/simple1.xml}
852 \li \c{/home/user/sources/myxmlparser/tests/tst_myxmlparser/testxml/simple1.xml}
853 \endlist
854
855 This allows the test to find its testdata regardless of whether the
856 test has been installed, and regardless of whether the test's build tree
857 is equal to the test's source tree.
858
859 \note reliable detection of testdata from the source directory requires
860 either that qmake is used, or the \c{QT_TESTCASE_BUILDDIR} macro is defined to
861 point to the working directory from which the compiler is invoked, or only
862 absolute paths to the source files are passed to the compiler. Otherwise, the
863 absolute path of the source directory cannot be determined.
864
865 \note The \c{QT_TESTCASE_BUILDDIR} macro is also implicitly defined if CMake is used
866 and the QtTest module is linked to the target. You can change the default
867 \c{QT_TESTCASE_BUILDDIR} by setting the QT_TESTCASE_BUILDDIR property on the target.
868
869 \note For tests that use the \l QTEST_APPLESS_MAIN() macro to generate a
870 \c{main()} function, \c{QFINDTESTDATA} will not attempt to find test data
871 relative to QCoreApplication::applicationDirPath(). In practice, this means that
872 tests using \c{QTEST_APPLESS_MAIN()} will fail to find their test data
873 if run from a shadow build tree.
874*/
875
876/*! \macro QTEST_MAIN(TestClass)
877
878 \relates QTest
879
880 Implements a main() function that instantiates an application object and
881 the \a TestClass, and executes all tests in the order they were defined.
882 Use this macro to build stand-alone executables.
883
884 If \c QT_WIDGETS_LIB is defined, the application object will be a QApplication,
885 if \c QT_GUI_LIB is defined, the application object will be a QGuiApplication,
886 otherwise it will be a QCoreApplication. If qmake is used and the configuration
887 includes \c{QT += widgets}, then \c QT_WIDGETS_LIB will be defined automatically.
888 Similarly, if qmake is used and the configuration includes \c{QT += gui}, then
889 \c QT_GUI_LIB will be defined automatically.
890
891 \note On platforms that have keypad navigation enabled by default,
892 this macro will forcefully disable it if \c QT_WIDGETS_LIB is defined. This is done
893 to simplify the usage of key events when writing autotests. If you wish to write a
894 test case that uses keypad navigation, you should enable it either in the
895 \c {initTestCase()} or \c {init()} functions of your test case by calling
896 \l {QApplication::setNavigationMode()}.
897
898 Example:
899 \snippet code/src_qtestlib_qtestcase.cpp 11
900
901 \sa QTEST_APPLESS_MAIN(), QTEST_GUILESS_MAIN(), QTest::qExec(),
902 QApplication::setNavigationMode()
903*/
904
905/*! \macro QTEST_APPLESS_MAIN(TestClass)
906
907 \relates QTest
908
909 Implements a main() function that executes all tests in \a TestClass.
910
911 Behaves like \l QTEST_MAIN(), but doesn't instantiate a QApplication
912 object. Use this macro for really simple stand-alone non-GUI tests.
913
914 \sa QTEST_MAIN()
915*/
916
917/*! \macro QTEST_GUILESS_MAIN(TestClass)
918 \since 5.0
919
920 \relates QTest
921
922 Implements a main() function that instantiates a QCoreApplication object
923 and the \a TestClass, and executes all tests in the order they were
924 defined. Use this macro to build stand-alone executables.
925
926 Behaves like \l QTEST_MAIN(), but instantiates a QCoreApplication instead
927 of the QApplication object. Use this macro if your test case doesn't need
928 functionality offered by QApplication, but the event loop is still necessary.
929
930 \sa QTEST_MAIN()
931*/
932
933/*!
934 \macro QBENCHMARK
935
936 \relates QTest
937
938 This macro is used to measure the performance of code within a test.
939 The code to be benchmarked is contained within a code block following
940 this macro.
941
942 For example:
943
944 \snippet code/src_qtestlib_qtestcase.cpp 27
945
946 \sa {Qt Test Overview#Creating a Benchmark}{Creating a Benchmark},
947 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
948*/
949
950/*!
951 \macro QBENCHMARK_ONCE
952 \since 4.6
953
954 \relates QTest
955
956 \brief The QBENCHMARK_ONCE macro is for measuring performance of a
957 code block by running it once.
958
959 This macro is used to measure the performance of code within a test.
960 The code to be benchmarked is contained within a code block following
961 this macro.
962
963 Unlike QBENCHMARK, the contents of the contained code block is only run
964 once. The elapsed time will be reported as "0" if it's to short to
965 be measured by the selected backend. (Use)
966
967 \sa {Qt Test Overview#Creating a Benchmark}{Creating a Benchmark},
968 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
969*/
970
971/*! \enum QTest::TestFailMode
972
973 This enum describes the modes for handling a check, such as by \l
974 QVERIFY() or \l QCOMPARE() macros, that is known to fail. The mode
975 applies regardless of whether the check fails or succeeds.
976
977 \value Abort Aborts the execution of the test. Use this mode when
978 it doesn't make sense to execute the test any further after
979 the problematic check.
980
981 \value Continue Continues execution of the test after the
982 problematic check.
983
984 \sa QEXPECT_FAIL()
985*/
986
987/*! \enum QTest::KeyAction
988
989 This enum describes possible actions for key handling.
990
991 \value Press The key is pressed.
992 \value Release The key is released.
993 \value Click The key is clicked (pressed and released).
994 \value Shortcut A shortcut is activated. This value has been added in Qt 5.6.
995*/
996
997/*! \enum QTest::MouseAction
998
999 This enum describes possible actions for mouse handling.
1000
1001 \value MousePress A mouse button is pressed.
1002 \value MouseRelease A mouse button is released.
1003 \value MouseClick A mouse button is clicked (pressed and released).
1004 \value MouseDClick A mouse button is double clicked (pressed and released twice).
1005 \value MouseMove The mouse pointer has moved.
1006*/
1007
1008/*! \fn void QTest::keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1009
1010 Simulates clicking of \a key with an optional \a modifier on a \a widget.
1011 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1012 before clicking the key.
1013
1014 Examples:
1015 \snippet code/src_qtestlib_qtestcase_snippet.cpp 14
1016
1017 The first example above simulates clicking the \c escape key on \c
1018 myWidget without any keyboard modifiers and without delay. The
1019 second example simulates clicking \c shift-escape on \c myWidget
1020 following a 200 ms delay of the test.
1021
1022 \sa QTest::keyClicks()
1023*/
1024
1025/*! \fn void QTest::keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1026 \overload
1027
1028 Simulates clicking of \a key with an optional \a modifier on a \a widget.
1029 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1030 before clicking the key.
1031
1032 Example:
1033 \snippet code/src_qtestlib_qtestcase_snippet.cpp 13
1034
1035 The example above simulates clicking \c a on \c myWidget without
1036 any keyboard modifiers and without delay of the test.
1037
1038 \sa QTest::keyClicks()
1039*/
1040
1041/*! \fn void QTest::keySequence(QWidget *widget, const QKeySequence &keySequence)
1042 \overload
1043 \since 5.10
1044
1045 Simulates typing of \a keySequence into a \a widget.
1046
1047 \sa QTest::keyClick(), QTest::keyClicks()
1048*/
1049
1050/*! \fn void QTest::keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1051 \overload
1052 \since 5.0
1053
1054 Simulates clicking of \a key with an optional \a modifier on a \a window.
1055 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1056 before clicking the key.
1057
1058 Examples:
1059 \snippet code/src_qtestlib_qtestcase_snippet.cpp 29
1060
1061 The first example above simulates clicking the \c escape key on \c
1062 myWindow without any keyboard modifiers and without delay. The
1063 second example simulates clicking \c shift-escape on \c myWindow
1064 following a 200 ms delay of the test.
1065
1066 \sa QTest::keyClicks()
1067*/
1068
1069/*! \fn void QTest::keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1070 \overload
1071 \since 5.0
1072
1073 Simulates clicking of \a key with an optional \a modifier on a \a window.
1074 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1075 before clicking the key.
1076
1077 Example:
1078 \snippet code/src_qtestlib_qtestcase_snippet.cpp 28
1079
1080 The example above simulates clicking \c a on \c myWindow without
1081 any keyboard modifiers and without delay of the test.
1082
1083 \sa QTest::keyClicks()
1084*/
1085
1086/*! \fn void QTest::keySequence(QWindow *window, const QKeySequence &keySequence)
1087 \overload
1088 \since 5.10
1089
1090 Simulates typing of \a keySequence into a \a window.
1091
1092 \sa QTest::keyClick(), QTest::keyClicks()
1093*/
1094
1095/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1096
1097 Sends a Qt key event to \a widget with the given \a key and an associated \a action.
1098 Optionally, a keyboard \a modifier can be specified, as well as a \a delay
1099 (in milliseconds) of the test before sending the event.
1100*/
1101
1102/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1103 \overload
1104
1105 Sends a Qt key event to \a widget with the given key \a ascii and an associated \a action.
1106 Optionally, a keyboard \a modifier can be specified, as well as a \a delay
1107 (in milliseconds) of the test before sending the event.
1108*/
1109
1110/*! \fn void QTest::keyEvent(KeyAction action, QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1111 \overload
1112 \since 5.0
1113
1114 Sends a Qt key event to \a window with the given \a key and an associated \a action.
1115 Optionally, a keyboard \a modifier can be specified, as well as a \a delay
1116 (in milliseconds) of the test before sending the event.
1117*/
1118
1119/*! \fn void QTest::keyEvent(KeyAction action, QWindow *window, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1120 \overload
1121 \since 5.0
1122
1123 Sends a Qt key event to \a window with the given key \a ascii and an associated \a action.
1124 Optionally, a keyboard \a modifier can be specified, as well as a \a delay
1125 (in milliseconds) of the test before sending the event.
1126*/
1127
1128/*! \fn void QTest::keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1129
1130 Simulates pressing a \a key with an optional \a modifier on a \a widget. If \a delay
1131 is larger than 0, the test will wait for \a delay milliseconds before pressing the key.
1132
1133 \note At some point you should release the key using \l keyRelease().
1134
1135 \sa QTest::keyRelease(), QTest::keyClick()
1136*/
1137
1138/*! \fn void QTest::keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1139 \overload
1140
1141 Simulates pressing a \a key with an optional \a modifier on a \a widget.
1142 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1143 before pressing the key.
1144
1145 \note At some point you should release the key using \l keyRelease().
1146
1147 \sa QTest::keyRelease(), QTest::keyClick()
1148*/
1149
1150/*! \fn void QTest::keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1151 \overload
1152 \since 5.0
1153
1154 Simulates pressing a \a key with an optional \a modifier on a \a window. If \a delay
1155 is larger than 0, the test will wait for \a delay milliseconds before pressing the key.
1156
1157 \note At some point you should release the key using \l keyRelease().
1158
1159 \sa QTest::keyRelease(), QTest::keyClick()
1160*/
1161
1162/*! \fn void QTest::keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1163 \overload
1164 \since 5.0
1165
1166 Simulates pressing a \a key with an optional \a modifier on a \a window.
1167 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1168 before pressing the key.
1169
1170 \note At some point you should release the key using \l keyRelease().
1171
1172 \sa QTest::keyRelease(), QTest::keyClick()
1173*/
1174
1175/*! \fn void QTest::keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1176
1177 Simulates releasing a \a key with an optional \a modifier on a \a widget.
1178 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1179 before releasing the key.
1180
1181 \sa QTest::keyPress(), QTest::keyClick()
1182*/
1183
1184/*! \fn void QTest::keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1185 \overload
1186
1187 Simulates releasing a \a key with an optional \a modifier on a \a widget.
1188 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1189 before releasing the key.
1190
1191 \sa QTest::keyClick()
1192*/
1193
1194/*! \fn void QTest::keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1195 \overload
1196 \since 5.0
1197
1198 Simulates releasing a \a key with an optional \a modifier on a \a window.
1199 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1200 before releasing the key.
1201
1202 \sa QTest::keyPress(), QTest::keyClick()
1203*/
1204
1205/*! \fn void QTest::keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1206 \overload
1207 \since 5.0
1208
1209 Simulates releasing a \a key with an optional \a modifier on a \a window.
1210 If \a delay is larger than 0, the test will wait for \a delay milliseconds
1211 before releasing the key.
1212
1213 \sa QTest::keyClick()
1214*/
1215
1216/*! \fn void QTest::keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
1217
1218 Simulates clicking a \a sequence of keys on a \a
1219 widget. Optionally, a keyboard \a modifier can be specified as
1220 well as a \a delay (in milliseconds) of the test before each key
1221 click.
1222
1223 Example:
1224 \snippet code/src_qtestlib_qtestcase_snippet.cpp 15
1225
1226 The example above simulates clicking the sequence of keys
1227 representing "hello world" on \c myWidget without any keyboard
1228 modifiers and without delay of the test.
1229
1230 \sa QTest::keyClick()
1231*/
1232
1233/*! \fn void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
1234
1235 Simulates pressing a mouse \a button with an optional \a modifier
1236 on a \a widget. The position is defined by \a pos; the default
1237 position is the center of the widget. If \a delay is specified,
1238 the test will wait for the specified amount of milliseconds before
1239 the press.
1240
1241 \sa QTest::mouseRelease(), QTest::mouseClick()
1242*/
1243
1244/*! \fn void QTest::mousePress(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
1245 \overload
1246 \since 5.0
1247
1248 Simulates pressing a mouse \a button with an optional \a stateKey modifier
1249 on a \a window. The position is defined by \a pos; the default
1250 position is the center of the window. If \a delay is specified,
1251 the test will wait for the specified amount of milliseconds before
1252 the press.
1253
1254 \sa QTest::mouseRelease(), QTest::mouseClick()
1255*/
1256
1257/*! \fn void QTest::mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
1258
1259 Simulates releasing a mouse \a button with an optional \a modifier
1260 on a \a widget. The position of the release is defined by \a pos;
1261 the default position is the center of the widget. If \a delay is
1262 specified, the test will wait for the specified amount of
1263 milliseconds before releasing the button; otherwise, it will wait for a
1264 default amount of time (1 ms), which can be overridden via
1265 \l {Testing Options}{command-line arguments}.
1266
1267 \note If you wish to test a double-click by sending events individually,
1268 specify a short delay, greater than the default, on both mouse release events.
1269 The total of the delays for the press, release, press and release must be
1270 less than QStyleHints::mouseDoubleClickInterval(). But if you don't need
1271 to check state between events, it's better to use QTest::mouseDClick().
1272 \snippet code/src_qtestlib_qtestcase_snippet.cpp 35
1273
1274 \sa QTest::mousePress(), QTest::mouseClick()
1275*/
1276
1277/*! \fn void QTest::mouseRelease(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
1278 \overload
1279 \since 5.0
1280
1281 Simulates releasing a mouse \a button with an optional \a stateKey modifier
1282 on a \a window. The position of the release is defined by \a pos;
1283 the default position is the center of the window. If \a delay is
1284 specified, the test will wait for the specified amount of
1285 milliseconds before releasing the button; otherwise, it will wait for a
1286 default amount of time (1 ms), which can be overridden via
1287 \l {Testing Options}{command-line arguments}.
1288
1289 \note If you wish to test a double-click by sending events individually,
1290 specify a short delay, greater than the default, on both mouse release events.
1291 The total of the delays for the press, release, press and release must be
1292 less than QStyleHints::mouseDoubleClickInterval(). But if you don't need
1293 to check state between events, it's better to use QTest::mouseDClick().
1294 \snippet code/src_qtestlib_qtestcase_snippet.cpp 35
1295
1296 \sa QTest::mousePress(), QTest::mouseClick()
1297*/
1298
1299/*! \fn void QTest::mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
1300
1301 Simulates clicking a mouse \a button with an optional \a modifier
1302 on a \a widget. The position of the click is defined by \a pos;
1303 the default position is the center of the widget. If \a delay is
1304 specified, the test will wait for the specified amount of
1305 milliseconds before pressing and before releasing the button.
1306
1307 \sa QTest::mousePress(), QTest::mouseRelease()
1308*/
1309
1310/*! \fn void QTest::mouseClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
1311 \overload
1312 \since 5.0
1313
1314 Simulates clicking a mouse \a button with an optional \a stateKey modifier
1315 on a \a window. The position of the click is defined by \a pos;
1316 the default position is the center of the window. If \a delay is
1317 specified, the test will wait for the specified amount of
1318 milliseconds before pressing and before releasing the button.
1319
1320 \sa QTest::mousePress(), QTest::mouseRelease()
1321*/
1322
1323/*! \fn void QTest::mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
1324
1325 Simulates double clicking a mouse \a button with an optional \a
1326 modifier on a \a widget. The position of the click is defined by
1327 \a pos; the default position is the center of the widget. If \a
1328 delay is specified, the test will wait for the specified amount of
1329 milliseconds before each press and release.
1330
1331 \sa QTest::mouseClick()
1332*/
1333
1334/*! \fn void QTest::mouseDClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
1335 \overload
1336 \since 5.0
1337
1338 Simulates double clicking a mouse \a button with an optional \a stateKey
1339 modifier on a \a window. The position of the click is defined by
1340 \a pos; the default position is the center of the window. If \a
1341 delay is specified, the test will wait for the specified amount of
1342 milliseconds before each press and release.
1343
1344 \sa QTest::mouseClick()
1345*/
1346
1347/*! \fn void QTest::mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1)
1348
1349 Moves the mouse pointer to a \a widget. If \a pos is not
1350 specified, the mouse pointer moves to the center of the widget. If
1351 a \a delay (in milliseconds) is given, the test will wait before
1352 moving the mouse pointer.
1353*/
1354
1355/*! \fn void QTest::mouseMove(QWindow *window, QPoint pos = QPoint(), int delay=-1)
1356 \overload
1357 \since 5.0
1358
1359 Moves the mouse pointer to a \a window. If \a pos is not
1360 specified, the mouse pointer moves to the center of the window. If
1361 a \a delay (in milliseconds) is given, the test will wait before
1362 moving the mouse pointer.
1363*/
1364
1365/*!
1366 \fn template <typename T1, typename T2> char *QTest::toString(const QPair<T1, T2> &pair)
1367 \overload
1368 \since 5.11
1369 Returns a textual representation of the \a pair.
1370*/
1371
1372/*!
1373 \fn template <typename T1, typename T2> char *QTest::toString(const std::pair<T1, T2> &pair)
1374 \overload
1375 \since 5.11
1376 Returns a textual representation of the \a pair.
1377*/
1378
1379/*!
1380 \fn char *QTest::toString(const QVector2D &v)
1381 \overload
1382 \since 5.11
1383 Returns a textual representation of the 2D vector \a v.
1384*/
1385
1386/*!
1387 \fn char *QTest::toString(const QVector3D &v)
1388 \overload
1389 \since 5.11
1390 Returns a textual representation of the 3D vector \a v.
1391*/
1392
1393/*!
1394 \fn char *QTest::toString(const QVector4D &v)
1395 \overload
1396 \since 5.11
1397 Returns a textual representation of the 4D vector \a v.
1398*/
1399
1400/*!
1401 \fn template<typename T> char *QTest::toString(const T &value)
1402
1403 Returns a textual representation of \a value. This function is used by
1404 \l QCOMPARE() to output verbose information in case of a test failure.
1405
1406 You can add specializations or overloads of this function to your test to enable
1407 verbose output.
1408
1409 \note Starting with Qt 5.5, you should prefer to provide a toString() function
1410 in the type's namespace instead of specializing this template.
1411 If your code needs to continue to work with the QTestLib from Qt 5.4 or
1412 earlier, you need to continue to use specialization.
1413
1414 \note The caller of toString() must delete the returned data
1415 using \c{delete[]}. Your implementation should return a string
1416 created with \c{new[]} or qstrdup(). The easiest way to do so is to
1417 create a QByteArray or QString and call QTest::toString() on it
1418 (see second example below).
1419
1420 Example for specializing (Qt ≤ 5.4):
1421
1422 \snippet code/src_qtestlib_qtestcase_snippet.cpp 16
1423
1424 The example above defines a toString() specialization for a class
1425 called \c MyPoint. Whenever a comparison of two instances of \c
1426 MyPoint fails, \l QCOMPARE() will call this function to output the
1427 contents of \c MyPoint to the test log.
1428
1429 Same example, but with overloading (Qt ≥ 5.5):
1430
1431 \snippet code/src_qtestlib_qtestcase_snippet.cpp toString-overload
1432
1433 \sa QCOMPARE()
1434*/
1435
1436/*!
1437 \fn char *QTest::toString(const QLatin1StringView &string)
1438 \overload
1439
1440 Returns a textual representation of the given \a string.
1441*/
1442
1443/*!
1444 \fn char *QTest::toString(std::nullptr_t)
1445 \overload
1446 \since 5.8
1447
1448 Returns a string containing \nullptr.
1449*/
1450
1451/*!
1452 \fn char *QTest::toString(const QStringView &string)
1453 \overload
1454 \since 5.11
1455
1456 Returns a textual representation of the given \a string.
1457*/
1458
1459/*!
1460 \fn char *QTest::toString(const QUuid &uuid)
1461 \overload
1462 \since 5.11
1463
1464 Returns a textual representation of the given \a uuid.
1465*/
1466
1467/*!
1468 \fn char *QTest::toString(const QString &string)
1469 \overload
1470
1471 Returns a textual representation of the given \a string.
1472*/
1473
1474/*!
1475 \fn char *QTest::toString(const QByteArray &ba)
1476 \overload
1477
1478 Returns a textual representation of the byte array \a ba.
1479
1480 \sa QTest::toHexRepresentation()
1481*/
1482
1483/*!
1484 \fn char *QTest::toString(const QCborError &c)
1485 \overload
1486 \since 5.12
1487
1488 Returns a textual representation of the given CBOR error \a c.
1489*/
1490
1491/*!
1492 \fn template <class... Types> char *QTest::toString(const std::tuple<Types...> &tuple)
1493 \overload
1494 \since 5.12
1495
1496 Returns a textual representation of the given \a tuple.
1497*/
1498
1499/*!
1500 \fn char *QTest::toString(const QTime &time)
1501 \overload
1502
1503 Returns a textual representation of the given \a time.
1504*/
1505
1506/*!
1507 \fn char *QTest::toString(const QDate &date)
1508 \overload
1509
1510 Returns a textual representation of the given \a date.
1511*/
1512
1513/*!
1514 \fn char *QTest::toString(const QDateTime &dateTime)
1515 \overload
1516
1517 Returns a textual representation of the date and time specified by
1518 \a dateTime.
1519*/
1520
1521/*!
1522 \fn char *QTest::toString(const QChar &character)
1523 \overload
1524
1525 Returns a textual representation of the given \a character.
1526*/
1527
1528/*!
1529 \fn char *QTest::toString(const QPoint &point)
1530 \overload
1531
1532 Returns a textual representation of the given \a point.
1533*/
1534
1535/*!
1536 \fn char *QTest::toString(const QSize &size)
1537 \overload
1538
1539 Returns a textual representation of the given \a size.
1540*/
1541
1542/*!
1543 \fn char *QTest::toString(const QRect &rectangle)
1544 \overload
1545
1546 Returns a textual representation of the given \a rectangle.
1547*/
1548
1549/*!
1550 \fn char *QTest::toString(const QUrl &url)
1551 \since 4.4
1552 \overload
1553
1554 Returns a textual representation of the given \a url.
1555*/
1556
1557/*!
1558 \fn char *QTest::toString(const QPointF &point)
1559 \overload
1560
1561 Returns a textual representation of the given \a point.
1562*/
1563
1564/*!
1565 \fn char *QTest::toString(const QSizeF &size)
1566 \overload
1567
1568 Returns a textual representation of the given \a size.
1569*/
1570
1571/*!
1572 \fn char *QTest::toString(const QRectF &rectangle)
1573 \overload
1574
1575 Returns a textual representation of the given \a rectangle.
1576*/
1577
1578/*!
1579 \fn char *QTest::toString(const QVariant &variant)
1580 \overload
1581
1582 Returns a textual representation of the given \a variant.
1583*/
1584
1585/*!
1586 \fn char *QTest::toString(QSizePolicy::ControlType ct)
1587 \overload
1588 \since 5.5
1589
1590 Returns a textual representation of control type \a ct.
1591*/
1592
1593/*!
1594 \fn char *QTest::toString(QSizePolicy::ControlTypes cts)
1595 \overload
1596 \since 5.5
1597
1598 Returns a textual representation of control types \a cts.
1599*/
1600
1601/*!
1602 \fn char *QTest::toString(QSizePolicy::Policy p)
1603 \overload
1604 \since 5.5
1605
1606 Returns a textual representation of policy \a p.
1607*/
1608
1609/*!
1610 \fn char *QTest::toString(QSizePolicy sp)
1611 \overload
1612 \since 5.5
1613
1614 Returns a textual representation of size policy \a sp.
1615*/
1616
1617/*!
1618 \fn char *QTest::toString(const QKeySequence &ks)
1619 \overload
1620 \since 6.5
1621 Returns a textual representation of the key sequence \a ks.
1622*/
1623
1624/*!
1625 \fn template <typename Tuple, int... I> char *QTest::toString(const Tuple &tuple, QtPrivate::IndexesList<I...> )
1626 \internal
1627 \since 5.12
1628*/
1629
1630/*!
1631 \fn QPointingDevice * QTest::createTouchDevice(QInputDevice::DeviceType devType = QInputDevice::DeviceType::TouchScreen, QInputDevice::Capabilities caps = QInputDevice::Capability::Position)
1632 \since 5.8
1633
1634 Creates a dummy touch device of type \a devType with capabilities \a caps for
1635 simulation of touch events.
1636
1637 The touch device will be registered with the QPA window system interface,
1638 and deleted automatically when the QCoreApplication is deleted. So you
1639 should typically use createTouchDevice() to initialize a QPointingDevice
1640 member variable in your test case class, and use the same instance for all tests.
1641
1642 \sa QTest::QTouchEventSequence, touchEvent()
1643*/
1644
1645/*!
1646 \class QTest::QTouchEventSequence
1647 \inmodule QtTest
1648 \since 4.6
1649
1650 \brief The QTouchEventSequence class is used to simulate a sequence of touch events.
1651
1652 To simulate a sequence of touch events on a specific device for a window or widget, call
1653 QTest::touchEvent to create a QTouchEventSequence instance. Add touch events to
1654 the sequence by calling press(), move(), release() and stationary(), and let the
1655 instance run out of scope to commit the sequence to the event system.
1656
1657 Example:
1658 \snippet code/src_qtestlib_qtestcase_snippet.cpp 25
1659*/
1660
1661/*!
1662 \fn QTest::QTouchEventSequence::~QTouchEventSequence()
1663
1664 Commits this sequence of touch events, unless autoCommit was disabled, and frees allocated resources.
1665*/
1666
1667/*!
1668 \fn bool QTest::QTouchEventSequence::commit(bool processEvents)
1669
1670 Commits this touch event to the event system, and returns whether it was
1671 accepted after delivery.
1672
1673 Normally there is no need to call this function because it is called from
1674 the destructor. However, if autoCommit is disabled, the events only get
1675 committed upon explicitly calling this function. Another reason to call it
1676 explicitly is to check the return value.
1677
1678 In special cases, tests may want to disable the processing of the event.
1679 This can be achieved by setting \a processEvents to false. This results in
1680 merely queuing the event: the event loop will not be forced to process it.
1681
1682 Returns whether the event was accepted after delivery.
1683*/
1684
1685/*!
1686 \fn QTouchEventSequence &QTest::QTouchEventSequence::press(int touchId, const QPoint &pt, QWindow *window)
1687 \since 5.0
1688
1689 Adds a press event for touchpoint \a touchId at position \a pt to this sequence and returns
1690 a reference to this QTouchEventSequence.
1691
1692 The position \a pt is interpreted as relative to \a window. If \a window is the null pointer, then
1693 \a pt is interpreted as relative to the window provided when instantiating this QTouchEventSequence.
1694
1695 Simulates that the user pressed the touch screen or pad with the finger identified by \a touchId.
1696*/
1697
1698/*!
1699 \fn QTouchEventWidgetSequence &QTest::QTouchEventWidgetSequence::press(int touchId, const QPoint &pt, QWidget *widget)
1700
1701 Adds a press event for touchpoint \a touchId at position \a pt to this sequence and returns
1702 a reference to this QTouchEventWidgetSequence.
1703
1704 The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then
1705 \a pt is interpreted as relative to the widget provided when instantiating this QTouchEventWidgetSequence.
1706
1707 Simulates that the user pressed the touch screen or pad with the finger identified by \a touchId.
1708*/
1709
1710/*!
1711 \fn QTouchEventSequence &QTest::QTouchEventSequence::move(int touchId, const QPoint &pt, QWindow *window)
1712 \since 5.0
1713
1714 Adds a move event for touchpoint \a touchId at position \a pt to this sequence and returns
1715 a reference to this QTouchEventSequence.
1716
1717 The position \a pt is interpreted as relative to \a window. If \a window is the null pointer, then
1718 \a pt is interpreted as relative to the window provided when instantiating this QTouchEventSequence.
1719
1720 Simulates that the user moved the finger identified by \a touchId.
1721*/
1722
1723/*!
1724 \fn QTouchEventWidgetSequence &QTest::QTouchEventWidgetSequence::move(int touchId, const QPoint &pt, QWidget *widget)
1725
1726 Adds a move event for touchpoint \a touchId at position \a pt to this sequence and returns
1727 a reference to this QTouchEventWidgetSequence.
1728
1729 The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then
1730 \a pt is interpreted as relative to the widget provided when instantiating this QTouchEventWidgetSequence.
1731
1732 Simulates that the user moved the finger identified by \a touchId.
1733*/
1734
1735/*!
1736 \fn QTouchEventSequence &QTest::QTouchEventSequence::release(int touchId, const QPoint &pt, QWindow *window)
1737 \since 5.0
1738
1739 Adds a release event for touchpoint \a touchId at position \a pt to this sequence and returns
1740 a reference to this QTouchEventSequence.
1741
1742 The position \a pt is interpreted as relative to \a window. If \a window is the null pointer, then
1743 \a pt is interpreted as relative to the window provided when instantiating this QTouchEventSequence.
1744
1745 Simulates that the user lifted the finger identified by \a touchId.
1746*/
1747
1748/*!
1749 \fn QTouchEventWidgetSequence &QTest::QTouchEventWidgetSequence::release(int touchId, const QPoint &pt, QWidget *widget)
1750
1751 Adds a release event for touchpoint \a touchId at position \a pt to this sequence and returns
1752 a reference to this QTouchEventWidgetSequence.
1753
1754 The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then
1755 \a pt is interpreted as relative to the widget provided when instantiating this QTouchEventWidgetSequence.
1756
1757 Simulates that the user lifted the finger identified by \a touchId.
1758*/
1759
1760/*!
1761 \fn QTouchEventSequence &QTest::QTouchEventSequence::stationary(int touchId)
1762
1763 Adds a stationary event for touchpoint \a touchId to this sequence and returns
1764 a reference to this QTouchEventSequence.
1765
1766 Simulates that the user did not move the finger identified by \a touchId.
1767*/
1768
1769/*!
1770 \fn QTouchEventSequence QTest::touchEvent(QWindow *window, QPointingDevice *device, bool autoCommit)
1771 \since 5.0
1772
1773 Creates and returns a QTouchEventSequence for the \a device to
1774 simulate events for \a window.
1775
1776 When adding touch events to the sequence, \a window will also be used to translate
1777 the position provided to screen coordinates, unless another window is provided in the
1778 respective calls to press(), move() etc.
1779
1780 The touch events are committed to the event system when the destructor of the
1781 QTouchEventSequence is called (ie when the object returned runs out of scope), unless
1782 \a autoCommit is set to false. When \a autoCommit is false, commit() has to be called
1783 manually.
1784
1785 \l createTouchDevice() can be called to create a test touch device for use with this
1786 function.
1787*/
1788
1789/*!
1790 \fn QTouchEventSequence QTest::touchEvent(QWidget *widget, QPointingDevice *device, bool autoCommit)
1791
1792 Creates and returns a QTouchEventSequence for the \a device to
1793 simulate events for \a widget.
1794
1795 When adding touch events to the sequence, \a widget will also be used to translate
1796 the position provided to screen coordinates, unless another widget is provided in the
1797 respective calls to press(), move() etc.
1798
1799 The touch events are committed to the event system when the destructor of the
1800 QTouchEventSequence is called (ie when the object returned runs out of scope), unless
1801 \a autoCommit is set to false. When \a autoCommit is false, commit() has to be called
1802 manually.
1803
1804 \l createTouchDevice() can be called to create a test touch device for use with this
1805 function.
1806*/
1807
1808// Internals of qtestmouse.h:
1809
1810/*! \fn void QTest::mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
1811 \internal
1812*/
1813
1814/*! \fn void QTest::mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
1815 \internal
1816*/