第 3 章:模拟 GUI 事件

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.

编写 GUI 测试

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:

#include <QtWidgets>
#include <QTest>
class TestGui: public QObject
{
    Q_OBJECT
private slots:
    void testGui();
};
					

The only difference is that you need to include the Qt GUI class definitions in addition to the QTest 名称空间。

void TestGui::testGui()
{
    QLineEdit lineEdit;
    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 QTest::keyClicks () 函数。

注意: The widget must also be shown in order to correctly test keyboard shortcuts.

QTest::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 QTest::keyClick (), QTest::keyPress (), QTest::keyRelease (), QTest::mouseClick (), QTest::mouseDClick (), QTest::mouseMove (), QTest::mousePress () 和 QTest::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)
#include "testgui.moc"
					

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.

构建采用 CMake

Configure your build settings in your CMakeLists.txt file:

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(tutorial3 LANGUAGES CXX)
find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)
qt_standard_project_setup()
qt_add_executable(tutorial3
    testgui.cpp
)
set_target_properties(tutorial3 PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)
target_link_libraries(tutorial3 PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Test
    Qt6::Widgets
)
install(TARGETS tutorial3
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
qt_generate_deploy_app_script(
    TARGET tutorial3
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
					

Next, from the command line, run either cmake 或使用 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:

ninja
					

采用 qmake 构建

Configure your build settings in your .pro 文件:

QT += widgets testlib
SOURCES = testgui.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial3
INSTALLS += target
					

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

qmake
make
					

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 **
					

第 2 章 第 4 章