1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
8 \brief The QTest namespace contains all the functions and
9 declarations that are related to Qt Test.
11 See the \l{Qt Test Overview} for information about how to write unit tests.
14/*! \macro QVERIFY(condition)
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.
22 You can use \l QVERIFY2() when it is practical and valuable to put additional
23 information into the test failure report.
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]
30 For example, the following code shows this macro being used to verify that a
31 \l QSignalSpy object is valid:
33 \snippet code/src_qtestlib_qtestcase_snippet.cpp 0
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.
39 \sa QCOMPARE(), QTRY_VERIFY(), QSignalSpy, QEXPECT_FAIL(), QCOMPARE_EQ(),
40 QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
43/*! \macro QVERIFY2(condition, message)
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.
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.
55 \snippet code/src_qtestlib_qtestcase.cpp 1
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:
60 \snippet code/src_qtestlib_qtestcase.cpp 32
62 If this test fails, it will give no clue as to why the file failed to open:
64 \c {FAIL! : tst_QFile::open_write() 'opened' returned FALSE. ()}
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:
70 \snippet code/src_qtestlib_qtestcase.cpp 33
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
76 \c {FAIL! : tst_QFile::open_write() 'opened' returned FALSE.
77 (open /tmp/qt.a3B42Cd: No space left on device)}
79 \sa QVERIFY(), QCOMPARE(), QEXPECT_FAIL(), QCOMPARE_EQ(), QCOMPARE_NE(),
80 QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
83/*! \macro QCOMPARE(actual, expected)
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.
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.
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.
103 \snippet code/src_qtestlib_qtestcase.cpp 2
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
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
116 Note that using initializer lists requires defining a helper macro
117 to prevent the preprocessor from interpreting the commas as macro argument
119 \snippet code/src_qtestlib_qtestcase.cpp 35
121 \include qtestcase.qdoc macro-usage-limitation
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]
129 \snippet code/src_qtestlib_qtestcase_snippet.cpp 34
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.
135 \sa QVERIFY(), QTRY_COMPARE(), QTest::toString(), QEXPECT_FAIL(),
136 QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(),
137 QCOMPARE_GT(), QCOMPARE_GE()
140/*! \macro QCOMPARE_EQ(computed, baseline)
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.
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.
154 \include qtestcase.qdoc macro-usage-limitation
156 \include qtestcase.qdoc to-string-overload-desc
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
163 \sa QCOMPARE(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(),
167/*! \macro QCOMPARE_NE(computed, baseline)
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.
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.
181 \include qtestcase.qdoc macro-usage-limitation
183 \include qtestcase.qdoc to-string-overload-desc
185 \sa QCOMPARE_EQ(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
188/*! \macro QCOMPARE_LT(computed, baseline)
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
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.
202 \include qtestcase.qdoc macro-usage-limitation
204 \include qtestcase.qdoc to-string-overload-desc
206 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LE(), QCOMPARE_GT(), QCOMPARE_GE()
209/*! \macro QCOMPARE_LE(computed, baseline)
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.
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.
223 \include qtestcase.qdoc macro-usage-limitation
225 \include qtestcase.qdoc to-string-overload-desc
227 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_GT(), QCOMPARE_GE()
230/*! \macro QCOMPARE_GT(computed, baseline)
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.
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.
244 \include qtestcase.qdoc macro-usage-limitation
246 \include qtestcase.qdoc to-string-overload-desc
248 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GE()
251/*! \macro QCOMPARE_GE(computed, baseline)
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.
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.
265 \include qtestcase.qdoc macro-usage-limitation
267 \include qtestcase.qdoc to-string-overload-desc
269 \sa QCOMPARE_EQ(), QCOMPARE_NE(), QCOMPARE_LT(), QCOMPARE_LE(), QCOMPARE_GT()
272/*! \macro QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)
276 \deprecated [6.3] Use \c{QVERIFY_THROWS_EXCEPTION(exceptiontype, expression)} instead.
280 \macro QVERIFY_THROWS_EXCEPTION(exceptiontype, ...)
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.
287 There are several possible outcomes:
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.
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).
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.
303 The macro uses variadic arguments so the expression can contain commas that the
304 preprocessor considers argument separators, e.g. as in
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 -----------------------/
313 \note This macro can only be used in a test function that is invoked
314 by the test framework.
318 \macro QVERIFY_THROWS_NO_EXCEPTION(...)
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.
326 There are several different outcomes:
329 \li If the expression does not throw an exception, then execution will continue.
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
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.
340 The macro uses variadic arguments so the expression can contain commas that the
341 preprocessor considers argument separators, e.g. as in
343 QVERIFY_THROWS_NO_EXCEPTION(std::pair<int, long>{42, 42L});
344 // macro arguments: \---- 1 ----/ \-- 2 -/ \3 /
347 \note This macro can only be used in a test function that is invoked
348 by the test framework.
351/*! \macro QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
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
362 \note This macro can only be used in a test function that is invoked
363 by the test framework.
365 \sa QTRY_VERIFY(), QTRY_VERIFY2_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
370/*! \macro QTRY_VERIFY(condition)
375 Checks the \a condition by invoking QTRY_VERIFY_WITH_TIMEOUT() with a timeout of five seconds.
377 \note This macro can only be used in a test function that is invoked
378 by the test framework.
380 \sa QTRY_VERIFY_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
384/*! \macro QTRY_VERIFY2_WITH_TIMEOUT(condition, message, timeout)
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.
395 QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10000);
398 \note This macro can only be used in a test function that is invoked
399 by the test framework.
401 \sa QTRY_VERIFY(), QTRY_VERIFY_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
405/*! \macro QTRY_VERIFY2(condition, message)
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.
416 QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData());
419 \note This macro can only be used in a test function that is invoked
420 by the test framework.
422 \sa QTRY_VERIFY2_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), QTRY_COMPARE(),
426/*! \macro QTRY_COMPARE_WITH_TIMEOUT(actual, expected, timeout)
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.
437 \note This macro can only be used in a test function that is invoked
438 by the test framework.
440 \sa QTRY_COMPARE(), QCOMPARE(), QVERIFY(), QTRY_VERIFY(), QEXPECT_FAIL()
443/*! \macro QTRY_COMPARE(actual, expected)
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.
451 \note This macro can only be used in a test function that is invoked
452 by the test framework.
454 \sa QTRY_COMPARE_WITH_TIMEOUT(), QCOMPARE(), QVERIFY(), QTRY_VERIFY(),
458/*! \macro QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout)
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.
468 \include qtestcase.qdoc macro-usage-limitation
470 \sa QCOMPARE_EQ(), QTRY_COMPARE_EQ()
473/*! \macro QTRY_COMPARE_EQ(computed, baseline)
477 Performs comparison of \a computed and \a baseline values by invoking
478 QTRY_COMPARE_EQ_WITH_TIMEOUT with a timeout of five seconds.
480 \include qtestcase.qdoc macro-usage-limitation
482 \sa QCOMPARE_EQ(), QTRY_COMPARE_EQ_WITH_TIMEOUT()
485/*! \macro QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout)
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.
495 \include qtestcase.qdoc macro-usage-limitation
497 \sa QCOMPARE_NE(), QTRY_COMPARE_NE()
500/*! \macro QTRY_COMPARE_NE(computed, baseline)
504 Performs comparison of \a computed and \a baseline values by invoking
505 QTRY_COMPARE_NE_WITH_TIMEOUT with a timeout of five seconds.
507 \include qtestcase.qdoc macro-usage-limitation
509 \sa QCOMPARE_NE(), QTRY_COMPARE_NE_WITH_TIMEOUT()
512/*! \macro QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout)
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.
522 \include qtestcase.qdoc macro-usage-limitation
524 \sa QCOMPARE_LT(), QTRY_COMPARE_LT()
527/*! \macro QTRY_COMPARE_LT(computed, baseline)
531 Performs comparison of \a computed and \a baseline values by invoking
532 QTRY_COMPARE_LT_WITH_TIMEOUT with a timeout of five seconds.
534 \include qtestcase.qdoc macro-usage-limitation
536 \sa QCOMPARE_LT(), QTRY_COMPARE_LT_WITH_TIMEOUT()
539/*! \macro QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout)
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.
549 \include qtestcase.qdoc macro-usage-limitation
551 \sa QCOMPARE_LE(), QTRY_COMPARE_LE()
554/*! \macro QTRY_COMPARE_LE(computed, baseline)
558 Performs comparison of \a computed and \a baseline values by invoking
559 QTRY_COMPARE_LE_WITH_TIMEOUT with a timeout of five seconds.
561 \include qtestcase.qdoc macro-usage-limitation
563 \sa QCOMPARE_LE(), QTRY_COMPARE_LE_WITH_TIMEOUT()
566/*! \macro QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout)
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.
576 \include qtestcase.qdoc macro-usage-limitation
578 \sa QCOMPARE_GT(), QTRY_COMPARE_GT()
581/*! \macro QTRY_COMPARE_GT(computed, baseline)
585 Performs comparison of \a computed and \a baseline values by invoking
586 QTRY_COMPARE_GT_WITH_TIMEOUT with a timeout of five seconds.
588 \include qtestcase.qdoc macro-usage-limitation
590 \sa QCOMPARE_GT(), QTRY_COMPARE_GT_WITH_TIMEOUT()
593/*! \macro QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout)
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.
603 \include qtestcase.qdoc macro-usage-limitation
605 \sa QCOMPARE_GE(), QTRY_COMPARE_GE()
608/*! \macro QTRY_COMPARE_GE(computed, baseline)
612 Performs comparison of \a computed and \a baseline values by invoking
613 QTRY_COMPARE_GE_WITH_TIMEOUT with a timeout of five seconds.
615 \include qtestcase.qdoc macro-usage-limitation
617 \sa QCOMPARE_GE(), QTRY_COMPARE_GE_WITH_TIMEOUT()
620/*! \macro QFETCH(type, name)
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.
628 Assuming a test has the following data:
630 \snippet code/src_qtestlib_qtestcase.cpp 3
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:
635 \snippet code/src_qtestlib_qtestcase.cpp 4
637 \c aString and \c expected are variables on the stack that are initialized with
638 the current test data.
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.
644/*! \macro QFETCH_GLOBAL(type, name)
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.
653 Assuming a test has the following data:
655 \snippet code/src_qtestlib_qtestcase_snippet.cpp 30
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).
664 \snippet code/src_qtestlib_qtestcase_snippet.cpp 31
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().
669 \note This macro can only be used in test methods of a class with an
670 \c initTestCase_data() method.
673/*! \macro QWARN(message)
677 \deprecated Use qWarning() instead.
679 Appends \a message as a warning to the test log. This macro can be used anywhere
683/*! \macro QFAIL(message)
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.
690 \note This macro can only be used in a test function that is invoked
691 by the test framework.
695 \snippet code/src_qtestlib_qtestcase.cpp 5
698/*! \macro QTEST(actual, testElement)
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.
706 Apart from that, QTEST() behaves exactly as \l QCOMPARE().
710 \snippet code/src_qtestlib_qtestcase.cpp 6
714 \snippet code/src_qtestlib_qtestcase.cpp 7
719/*! \macro QSKIP(description)
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
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.
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.
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.
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().
751 \note This macro can only be used in a test function or \c _data
752 function that is invoked by the test framework.
755 \snippet code/src_qtestlib_qtestcase.cpp 8
757 \section2 Skipping Known Bugs
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.
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
773 \sa QEXPECT_FAIL(), {Select Appropriate Mechanisms to Exclude Tests}
776/*! \macro QEXPECT_FAIL(dataIndex, comment, mode)
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.
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.
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.
791 \a comment will be appended to the test log for the expected failure.
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.
797 \note This macro can only be used in a test function that is invoked
798 by the test framework.
801 \snippet code/src_qtestlib_qtestcase.cpp 9
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.
809 \snippet code/src_qtestlib_qtestcase.cpp 10
811 The above testfunction will not continue executing for the test data
812 entry \c{data27} (regardless of the value of \c i).
814 \sa QTest::TestFailMode, QVERIFY(), QCOMPARE()
817/*! \macro QFINDTESTDATA(filename)
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.
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.
829 The returned path will be the first path from the following list which
830 resolves to an existing file or directory:
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.
841 If the named file/directory does not exist at any of these locations,
842 a warning is printed to the test log.
844 For example, in this code:
845 \snippet code/src_qtestlib_qtestcase_snippet.cpp 26
847 The testdata file will be resolved as the first existing file from:
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}
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.
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.
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.
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.
876/*! \macro QTEST_MAIN(TestClass)
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.
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.
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()}.
899 \snippet code/src_qtestlib_qtestcase.cpp 11
901 \sa QTEST_APPLESS_MAIN(), QTEST_GUILESS_MAIN(), QTest::qExec(),
902 QApplication::setNavigationMode()
905/*! \macro QTEST_APPLESS_MAIN(TestClass)
909 Implements a main() function that executes all tests in \a TestClass.
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.
917/*! \macro QTEST_GUILESS_MAIN(TestClass)
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.
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.
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
944 \snippet code/src_qtestlib_qtestcase.cpp 27
946 \sa {Qt Test Overview#Creating a Benchmark}{Creating a Benchmark},
947 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
951 \macro QBENCHMARK_ONCE
956 \brief The QBENCHMARK_ONCE macro is for measuring performance of a
957 code block by running it once.
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
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)
967 \sa {Qt Test Overview#Creating a Benchmark}{Creating a Benchmark},
968 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
971/*! \enum QTest::TestFailMode
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.
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.
981 \value Continue Continues execution of the test after the
987/*! \enum QTest::KeyAction
989 This enum describes possible actions for key handling.
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.
997/*! \enum QTest::MouseAction
999 This enum describes possible actions for mouse handling.
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.
1008/*! \fn void QTest::keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1015 \snippet code/src_qtestlib_qtestcase_snippet.cpp 14
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.
1022 \sa QTest::keyClicks()
1025/*! \fn void QTest::keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1033 \snippet code/src_qtestlib_qtestcase_snippet.cpp 13
1035 The example above simulates clicking \c a on \c myWidget without
1036 any keyboard modifiers and without delay of the test.
1038 \sa QTest::keyClicks()
1041/*! \fn void QTest::keySequence(QWidget *widget, const QKeySequence &keySequence)
1045 Simulates typing of \a keySequence into a \a widget.
1047 \sa QTest::keyClick(), QTest::keyClicks()
1050/*! \fn void QTest::keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1059 \snippet code/src_qtestlib_qtestcase_snippet.cpp 29
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.
1066 \sa QTest::keyClicks()
1069/*! \fn void QTest::keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1078 \snippet code/src_qtestlib_qtestcase_snippet.cpp 28
1080 The example above simulates clicking \c a on \c myWindow without
1081 any keyboard modifiers and without delay of the test.
1083 \sa QTest::keyClicks()
1086/*! \fn void QTest::keySequence(QWindow *window, const QKeySequence &keySequence)
1090 Simulates typing of \a keySequence into a \a window.
1092 \sa QTest::keyClick(), QTest::keyClicks()
1095/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1102/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1110/*! \fn void QTest::keyEvent(KeyAction action, QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1119/*! \fn void QTest::keyEvent(KeyAction action, QWindow *window, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1128/*! \fn void QTest::keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1133 \note At some point you should release the key using \l keyRelease().
1135 \sa QTest::keyRelease(), QTest::keyClick()
1138/*! \fn void QTest::keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1145 \note At some point you should release the key using \l keyRelease().
1147 \sa QTest::keyRelease(), QTest::keyClick()
1150/*! \fn void QTest::keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1157 \note At some point you should release the key using \l keyRelease().
1159 \sa QTest::keyRelease(), QTest::keyClick()
1162/*! \fn void QTest::keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1170 \note At some point you should release the key using \l keyRelease().
1172 \sa QTest::keyRelease(), QTest::keyClick()
1175/*! \fn void QTest::keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1181 \sa QTest::keyPress(), QTest::keyClick()
1184/*! \fn void QTest::keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1191 \sa QTest::keyClick()
1194/*! \fn void QTest::keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1202 \sa QTest::keyPress(), QTest::keyClick()
1205/*! \fn void QTest::keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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.
1213 \sa QTest::keyClick()
1216/*! \fn void QTest::keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
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
1224 \snippet code/src_qtestlib_qtestcase_snippet.cpp 15
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.
1230 \sa QTest::keyClick()
1233/*! \fn void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
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
1241 \sa QTest::mouseRelease(), QTest::mouseClick()
1244/*! \fn void QTest::mousePress(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
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
1254 \sa QTest::mouseRelease(), QTest::mouseClick()
1257/*! \fn void QTest::mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
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}.
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
1274 \sa QTest::mousePress(), QTest::mouseClick()
1277/*! \fn void QTest::mouseRelease(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
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}.
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
1296 \sa QTest::mousePress(), QTest::mouseClick()
1299/*! \fn void QTest::mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
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.
1307 \sa QTest::mousePress(), QTest::mouseRelease()
1310/*! \fn void QTest::mouseClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
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.
1320 \sa QTest::mousePress(), QTest::mouseRelease()
1323/*! \fn void QTest::mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
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.
1331 \sa QTest::mouseClick()
1334/*! \fn void QTest::mouseDClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay=-1)
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.
1344 \sa QTest::mouseClick()
1347/*! \fn void QTest::mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1)
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.
1355/*! \fn void QTest::mouseMove(QWindow *window, QPoint pos = QPoint(), int delay=-1)
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.
1366 \fn template <typename T1, typename T2> char *QTest::toString(const QPair<T1, T2> &pair)
1369 Returns a textual representation of the \a pair.
1373 \fn template <typename T1, typename T2> char *QTest::toString(const std::pair<T1, T2> &pair)
1376 Returns a textual representation of the \a pair.
1380 \fn char *QTest::toString(const QVector2D &v)
1383 Returns a textual representation of the 2D vector \a v.
1387 \fn char *QTest::toString(const QVector3D &v)
1390 Returns a textual representation of the 3D vector \a v.
1394 \fn char *QTest::toString(const QVector4D &v)
1397 Returns a textual representation of the 4D vector \a v.
1401 \fn template<typename T> char *QTest::toString(const T &value)
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.
1406 You can add specializations or overloads of this function to your test to enable
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.
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).
1420 Example for specializing (Qt ≤ 5.4):
1422 \snippet code/src_qtestlib_qtestcase_snippet.cpp 16
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.
1429 Same example, but with overloading (Qt ≥ 5.5):
1431 \snippet code/src_qtestlib_qtestcase_snippet.cpp toString-overload
1437 \fn char *QTest::toString(const QLatin1StringView &string)
1440 Returns a textual representation of the given \a string.
1444 \fn char *QTest::toString(std::nullptr_t)
1448 Returns a string containing \nullptr.
1452 \fn char *QTest::toString(const QStringView &string)
1456 Returns a textual representation of the given \a string.
1460 \fn char *QTest::toString(const QUuid &uuid)
1464 Returns a textual representation of the given \a uuid.
1468 \fn char *QTest::toString(const QString &string)
1471 Returns a textual representation of the given \a string.
1475 \fn char *QTest::toString(const QByteArray &ba)
1478 Returns a textual representation of the byte array \a ba.
1480 \sa QTest::toHexRepresentation()
1484 \fn char *QTest::toString(const QCborError &c)
1488 Returns a textual representation of the given CBOR error \a c.
1492 \fn template <class... Types> char *QTest::toString(const std::tuple<Types...> &tuple)
1496 Returns a textual representation of the given \a tuple.
1500 \fn char *QTest::toString(const QTime &time)
1503 Returns a textual representation of the given \a time.
1507 \fn char *QTest::toString(const QDate &date)
1510 Returns a textual representation of the given \a date.
1514 \fn char *QTest::toString(const QDateTime &dateTime)
1517 Returns a textual representation of the date and time specified by
1522 \fn char *QTest::toString(const QChar &character)
1525 Returns a textual representation of the given \a character.
1529 \fn char *QTest::toString(const QPoint &point)
1532 Returns a textual representation of the given \a point.
1536 \fn char *QTest::toString(const QSize &size)
1539 Returns a textual representation of the given \a size.
1543 \fn char *QTest::toString(const QRect &rectangle)
1546 Returns a textual representation of the given \a rectangle.
1550 \fn char *QTest::toString(const QUrl &url)
1554 Returns a textual representation of the given \a url.
1558 \fn char *QTest::toString(const QPointF &point)
1561 Returns a textual representation of the given \a point.
1565 \fn char *QTest::toString(const QSizeF &size)
1568 Returns a textual representation of the given \a size.
1572 \fn char *QTest::toString(const QRectF &rectangle)
1575 Returns a textual representation of the given \a rectangle.
1579 \fn char *QTest::toString(const QVariant &variant)
1582 Returns a textual representation of the given \a variant.
1586 \fn char *QTest::toString(QSizePolicy::ControlType ct)
1590 Returns a textual representation of control type \a ct.
1594 \fn char *QTest::toString(QSizePolicy::ControlTypes cts)
1598 Returns a textual representation of control types \a cts.
1602 \fn char *QTest::toString(QSizePolicy::Policy p)
1606 Returns a textual representation of policy \a p.
1610 \fn char *QTest::toString(QSizePolicy sp)
1614 Returns a textual representation of size policy \a sp.
1618 \fn char *QTest::toString(const QKeySequence &ks)
1621 Returns a textual representation of the key sequence \a ks.
1625 \fn template <typename Tuple, int... I> char *QTest::toString(const Tuple &tuple, QtPrivate::IndexesList<I...> )
1631 \fn QPointingDevice * QTest::createTouchDevice(QInputDevice::DeviceType devType = QInputDevice::DeviceType::TouchScreen, QInputDevice::Capabilities caps = QInputDevice::Capability::Position)
1634 Creates a dummy touch device of type \a devType with capabilities \a caps for
1635 simulation of touch events.
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.
1642 \sa QTest::QTouchEventSequence, touchEvent()
1646 \class QTest::QTouchEventSequence
1650 \brief The QTouchEventSequence class is used to simulate a sequence of touch events.
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.
1658 \snippet code/src_qtestlib_qtestcase_snippet.cpp 25
1662 \fn QTest::QTouchEventSequence::~QTouchEventSequence()
1664 Commits this sequence of touch events, unless autoCommit was disabled, and frees allocated resources.
1668 \fn bool QTest::QTouchEventSequence::commit(bool processEvents)
1670 Commits this touch event to the event system, and returns whether it was
1671 accepted after delivery.
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.
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.
1682 Returns whether the event was accepted after delivery.
1686 \fn QTouchEventSequence &QTest::QTouchEventSequence::press(int touchId, const QPoint &pt, QWindow *window)
1689 Adds a press event for touchpoint \a touchId at position \a pt to this sequence and returns
1690 a reference to this QTouchEventSequence.
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.
1695 Simulates that the user pressed the touch screen or pad with the finger identified by \a touchId.
1699 \fn QTouchEventWidgetSequence &QTest::QTouchEventWidgetSequence::press(int touchId, const QPoint &pt, QWidget *widget)
1701 Adds a press event for touchpoint \a touchId at position \a pt to this sequence and returns
1702 a reference to this QTouchEventWidgetSequence.
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.
1707 Simulates that the user pressed the touch screen or pad with the finger identified by \a touchId.
1711 \fn QTouchEventSequence &QTest::QTouchEventSequence::move(int touchId, const QPoint &pt, QWindow *window)
1714 Adds a move event for touchpoint \a touchId at position \a pt to this sequence and returns
1715 a reference to this QTouchEventSequence.
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.
1720 Simulates that the user moved the finger identified by \a touchId.
1724 \fn QTouchEventWidgetSequence &QTest::QTouchEventWidgetSequence::move(int touchId, const QPoint &pt, QWidget *widget)
1726 Adds a move event for touchpoint \a touchId at position \a pt to this sequence and returns
1727 a reference to this QTouchEventWidgetSequence.
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.
1732 Simulates that the user moved the finger identified by \a touchId.
1736 \fn QTouchEventSequence &QTest::QTouchEventSequence::release(int touchId, const QPoint &pt, QWindow *window)
1739 Adds a release event for touchpoint \a touchId at position \a pt to this sequence and returns
1740 a reference to this QTouchEventSequence.
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.
1745 Simulates that the user lifted the finger identified by \a touchId.
1749 \fn QTouchEventWidgetSequence &QTest::QTouchEventWidgetSequence::release(int touchId, const QPoint &pt, QWidget *widget)
1751 Adds a release event for touchpoint \a touchId at position \a pt to this sequence and returns
1752 a reference to this QTouchEventWidgetSequence.
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.
1757 Simulates that the user lifted the finger identified by \a touchId.
1761 \fn QTouchEventSequence &QTest::QTouchEventSequence::stationary(int touchId)
1763 Adds a stationary event for touchpoint \a touchId to this sequence and returns
1764 a reference to this QTouchEventSequence.
1766 Simulates that the user did not move the finger identified by \a touchId.
1770 \fn QTouchEventSequence QTest::touchEvent(QWindow *window, QPointingDevice *device, bool autoCommit)
1773 Creates and returns a QTouchEventSequence for the \a device to
1774 simulate events for \a window.
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.
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
1785 \l createTouchDevice() can be called to create a test touch device for use with this
1790 \fn QTouchEventSequence QTest::touchEvent(QWidget *widget, QPointingDevice *device, bool autoCommit)
1792 Creates and returns a QTouchEventSequence for the \a device to
1793 simulate events for \a widget.
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.
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
1804 \l createTouchDevice() can be called to create a test touch device for use with this
1808// Internals of qtestmouse.h:
1810/*! \fn void QTest::mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
1814/*! \fn void QTest::mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)