本页内容

第 3 章:模拟 GUI 事件

Qt Test 介绍了用于测试图形用户界面的若干机制。Qt Test 不会模拟原生窗口系统事件,而是发送内部 Qt 事件。这意味着测试在运行机器上不会产生任何副作用。

本章将演示如何编写一个简单的 GUI 测试。

编写 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() 函数来模拟相关的 GUI 事件。

最后,我们使用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 **

© 2026 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.