第 3 章:模拟图形用户界面事件

Qt Test 图形用户界面测试的一些机制。 发送内部 Qt 事件,而不是模拟本地窗口系统事件。这意味着不会对测试运行的机器产生副作用。Qt Test

本章将演示如何编写一个简单的图形用户界面测试。

编写 GUI 测试

这次,假设你要测试我们的QLineEdit 类的行为。和以前一样,你需要一个包含测试函数的类:

#include <QtWidgets>
#include <QTest>

class TestGui: public QObject
{
    Q_OBJECT

private slots:
    void testGui();

};

唯一不同的是,除了QTest 命名空间外,您还需要包含Qt GUI 类定义。

void TestGui::testGui()
{
    QLineEdit lineEdit;

    QTest::keyClicks(&lineEdit, "hello world");

    QCOMPARE(lineEdit.text(), QString("hello world"));
}

在测试函数的实现过程中,我们首先创建一个QLineEdit 。然后,我们使用QTest::keyClicks() 函数模拟在行编辑中写入 "hello world"。

注意: 为了正确测试键盘快捷键,还必须显示部件。

QTest::keyClicks() 模拟点击部件上的一连串按键。可选择指定键盘修改器以及每次按键后的测试延迟(以毫秒为单位)。同样,您也可以使用QTest::keyClick(),QTest::keyPress(),QTest::keyRelease(),QTest::mouseClick(),QTest::mouseDClick(),QTest::mouseMove(),QTest::mousePress() 和QTest::mouseRelease() 函数来模拟相关的图形用户界面事件。

最后,我们使用QCOMPARE() 宏来检查行编辑的文本是否符合预期。

准备单机版可执行文件

与之前一样,要使我们的测试用例成为独立的可执行文件,需要以下两行:

QTEST_MAIN(TestGui)
#include "testgui.moc"

QTEST_MAIN() 宏扩展为一个简单的 main() 方法,用于运行所有测试函数。由于测试类的声明和实现都在 .cpp 文件中,我们还需要包含生成的 moc 文件,以使 Qt 的自省功能正常工作。

构建可执行文件

您可以使用 CMake 或 qmake 构建测试用例可执行文件。

使用 CMake 构建

在 CMakeLists.txt 文件中配置构建设置:

# 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})

然后,从命令行运行cmake 或使用Qt-prefix/<version>/<platform>/bin/qt-cmake 中的qt-cmake 方便脚本:

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

然后,运行你喜欢的生成工具来构建可执行文件。在这里,我们使用的是 Ninja:

ninja

使用 qmake

.pro 文件中配置编译设置:

QT += widgets testlib

SOURCES = testgui.cpp

# install
target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial3
INSTALLS += target

然后,运行qmake ,最后运行make 来构建可执行文件:

qmake
make

运行可执行文件

运行生成的可执行文件会有如下输出:

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

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.