本页内容

Qt Test

Qt Test 模块为Qt应用程序和库的单元测试及基准测试提供了C++ API。大部分API位于QTest 命名空间中。此外,QSignalSpy 类提供了对Qt信号与槽的便捷内省功能,而QAbstractItemModelTester 则支持对项模型进行非破坏性测试。

注意: Qt Test 模块不保证 二进制兼容性。这意味着使用Qt Test 的应用程序仅保证能在开发时所基于的Qt版本上运行。不过,源代码兼容性是得到保证的。

使用该模块

要使用 Qt 模块的 C++ API,需要将模块库链接到项目中,既可以直接链接,也可以通过其他依赖项间接链接。包括CMake和qmake 在内的多种构建工具都为此提供了专门的支持。

使用 CMake 构建

使用find_package() 命令在Qt6 包中定位所需的模块组件:

find_package(Qt6 REQUIRED COMPONENTS Test)
target_link_libraries(mytarget PRIVATE Qt6::Test)

更多详细信息,请参阅“使用 CMake 构建”概述。

使用 qmake 构建

要配置模块以便使用 qmake 进行构建,请在项目的 .pro 文件中将模块作为QT 变量的值添加:

QT += testlib

文章与指南

参考

该 Qt Quick Test 模块支持对Qt Quick 应用程序进行单元测试。

模块演变

Qt Test 中的“更改”部分列出了针对 Qt 6 系列所做的模块 API 和功能方面的重大更改。

许可与归属

Qt Test 由The Qt Company 提供商业许可。此外,它还提供自由软件许可:GNU 较少通用公共许可证第 3 版或GNU 通用公共许可证第 2 版。更多详细信息请参阅Qt 许可。

此外,Qt 6.12.0 中的Qt Test 可能包含采用以下宽松许可协议的第三方模块:

Catch2, version 2.13.10

Boost 软件许可协议 1.0

Cycle, version 3.3.11

MIT 许可

Linux Performance Events, version 7.0

GNU 通用公共许可证 v2.0(仅限包含 Linux 系统调用注释的版本)

Valgrind, version 3.25.1

BSD 4 条款“原始”或“旧版”许可

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