This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Chapter 4: Replaying GUI Events#

How to replay GUI events.

In this chapter, we will show how to simulate a GUI event, and how to store a series of GUI events as well as replay them on a widget.

The approach to storing a series of events and replaying them is quite similar to the approach explained in chapter 2 . All you need to do is to add a data function to your test class:

class TestGui(QObject):

# private slots
    def testGui_data():
    def testGui():

Writing the Data Function#

As before, a test function’s associated data function carries the same name, appended by _data.

def testGui_data(self):

    list1 = QTestEventList()
    QTest.newRow("char") << list1 << "a"
    list2 = QTestEventList()
    QTest.newRow("there+back-again") << list2 << ""

First, we define the elements of the table using the addColumn() function: A list of GUI events, and the expected result of applying the list of events on a QWidget . Note that the type of the first element is QTestEventList .

A QTestEventList can be populated with GUI events that can be stored as test data for later usage, or be replayed on any QWidget .

In our current data function, we create two QTestEventList elements. The first list consists of a single click to the ‘a’ key. We add the event to the list using the addKeyClick() function. Then we use the newRow() function to give the data set a name, and stream the event list and the expected result into the table.

The second list consists of two key clicks: an ‘a’ with a following ‘backspace’. Again we use the addKeyClick() to add the events to the list, and newRow() to put the event list and the expected result into the table with an associated name.

Rewriting the Test Function#

Our test can now be rewritten:

def testGui(self):

    QFETCH(QTestEventList, events)
    QFETCH(QString, expected)
    lineEdit = QLineEdit()
    QCOMPARE(lineEdit.text(), expected)

The TestGui::testGui() function will be executed two times, once for each entry in the test data that we created in the associated TestGui::testGui_data() function.

First, we fetch the two elements of the data set using the QFETCH() macro. QFETCH() takes two arguments: the data type of the element and the element name. Then we create a QLineEdit , and apply the list of events on that widget using the simulate() function.

Finally, we use the QCOMPARE() macro to check if the line edit’s text is as expected.

Preparing the Stand-Alone Executable#

As before, to make our test case a stand-alone executable, the following two lines are needed:

from testgui.moc import *

The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions, and since both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt’s introspection work.

Building the Executable#

You can build the test case executable using CMake or qmake.

Building with CMake#

Configure your build settings in your CMakeLists.txt file:

<Code snippet "/data/qt5-full-650/6.5.0/Src/qtbase/tutorial4/CMakeLists.txt" not found>

Next, from the command line, run either cmake or use the qt-cmake convenience script located in Qt-prefix/<version>/<platform>/bin/qt-cmake:

<Qt-prefix>/<version>/<platform>/bin/qt-cmake <source-dir> <build-dir> -G Ninja

Then, run your preferred generator tool to build the executable. Here, we’re using Ninja:


Building with qmake#

Configure your build settings in your .pro file:

<Code snippet "/data/qt5-full-650/6.5.0/Src/qtbase/tutorial4/tutorial4.pro" not found>

Next, run qmake, and, finally, run make to build your executable:


Running the Executable#

Running the resulting executable should give you the following output:

********* Start testing of TestGui *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS   : TestGui::initTestCase()
PASS   : TestGui::testGui(char)
PASS   : TestGui::testGui(there+back-again)
PASS   : TestGui::cleanupTestCase()
Totals: 4 passed, 0 failed, 0 skipped, 0 blacklisted, 18ms
********* Finished testing of TestGui *********

Example project @ code.qt.io