This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Chapter 3: Simulating GUI Events#
Howe to simulate GUI events.
Qt Test features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, Qt Test sends internal Qt events. That means there are no side-effects on the machine the tests are running on.
This chapter demonstrates how to write a simple GUI test.
Writing a GUI Test#
This time, let’s assume you want to test the behavior of our
QLineEdit class. As before, you will need a class that contains your test function:
from PySide6 import QtWidgets from PySide6.QtTest import QTest class TestGui(QObject): Q_OBJECT # private slots def testGui():
The only difference is that you need to include the Qt GUI class definitions in addition to the QTest namespace.
def testGui(self): lineEdit = QLineEdit() QTest.keyClicks(lineEdit, "hello world") QCOMPARE(lineEdit.text(), QString("hello world"))
In the implementation of the test function, we first create a
QLineEdit . Then, we simulate writing “hello world” in the line edit using the
The widget must also be shown in order to correctly test keyboard shortcuts.
keyClicks() simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test after each key click. In a similar way, you can use the
mouseRelease() functions to simulate the associated GUI events.
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:
QTEST_MAIN(TestGui) from testgui.moc import *
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/tutorial3/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 <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
<Code snippet "/data/qt5-full-650/6.5.0/Src/qtbase/tutorial3/tutorial3.pro" not found>
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() PASS : TestGui::cleanupTestCase() Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 20ms ********* Finished testing of TestGui **