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
6 \page qttestlib-tutorial3-example.html
7 \previouspage {Chapter 2: Data Driven Testing}{Chapter 2}
8 \nextpage {Chapter 4: Replaying GUI Events}{Chapter 4}
10 \title Chapter 3: Simulating GUI Events
11 \brief How to simulate GUI events.
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.
18 This chapter demonstrates how to write a simple GUI test.
20 \section1 Writing a GUI Test
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
26 \snippet tutorial3/testgui.cpp 0
28 The only difference is that you need to include the Qt GUI class
29 definitions in addition to the QTest namespace.
31 \snippet tutorial3/testgui.cpp 1
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.
37 \note The widget must also be shown in order to correctly test keyboard
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
49 Finally, we use the \l QCOMPARE() macro to check if the line edit's
52 \section1 Preparing the Stand-Alone Executable
54 As before, to make our test case a stand-alone executable, the
55 following two lines are needed:
57 \snippet tutorial3/testgui.cpp 2
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
65 \section1 Building the Executable
67 \include {building-examples.qdocinc} {building the executable} {tutorial3}
69 \section1 Running the Executable
71 Running the resulting executable should give you the following
74 \snippet code/doc_src_qtestlib.qdoc 12