Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qttestlib-tutorial3.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5/*!
6 \page qttestlib-tutorial3-example.html
7 \previouspage {Chapter 2: Data Driven Testing}{Chapter 2}
8 \nextpage {Chapter 4: Replaying GUI Events}{Chapter 4}
9
10 \title Chapter 3: Simulating GUI Events
11 \brief How to simulate GUI events.
12
13 Qt Test features some mechanisms to test graphical user
14 interfaces. Instead of simulating native window system events,
15 Qt Test sends internal Qt events. That means there are no
16 side-effects on the machine the tests are running on.
17
18 This chapter demonstrates how to write a simple GUI test.
19
20 \section1 Writing a GUI Test
21
22 This time, let's assume you want to test the behavior of our
23 QLineEdit class. As before, you will need a class that contains
24 your test function:
25
26 \snippet tutorial3/testgui.cpp 0
27
28 The only difference is that you need to include the Qt GUI class
29 definitions in addition to the QTest namespace.
30
31 \snippet tutorial3/testgui.cpp 1
32
33 In the implementation of the test function, we first create a
34 QLineEdit. Then, we simulate writing "hello world" in the line edit
35 using the \l QTest::keyClicks() function.
36
37 \note The widget must also be shown in order to correctly test keyboard
38 shortcuts.
39
40 QTest::keyClicks() simulates clicking a sequence of keys on a
41 widget. Optionally, a keyboard modifier can be specified as well
42 as a delay (in milliseconds) of the test after each key click. In
43 a similar way, you can use the QTest::keyClick(),
44 QTest::keyPress(), QTest::keyRelease(), QTest::mouseClick(),
45 QTest::mouseDClick(), QTest::mouseMove(), QTest::mousePress()
46 and QTest::mouseRelease() functions to simulate the associated
47 GUI events.
48
49 Finally, we use the \l QCOMPARE() macro to check if the line edit's
50 text is as expected.
51
52 \section1 Preparing the Stand-Alone Executable
53
54 As before, to make our test case a stand-alone executable, the
55 following two lines are needed:
56
57 \snippet tutorial3/testgui.cpp 2
58
59 The QTEST_MAIN() macro expands to a simple main() method that
60 runs all the test functions, and since both the declaration and
61 the implementation of our test class are in a .cpp file, we also
62 need to include the generated moc file to make Qt's introspection
63 work.
64
65 \section1 Building the Executable
66
67 \include {building-examples.qdocinc} {building the executable} {tutorial3}
68
69 \section1 Running the Executable
70
71 Running the resulting executable should give you the following
72 output:
73
74 \snippet code/doc_src_qtestlib.qdoc 12
75*/