创建 Qt Test
Qt Creator 集成了 Qt Test 用于对 Qt 应用程序和库进行单元测试的框架。
注意: Qt 5 或更高版本支持运行 Qt Test 并显示其结果。
要创建 Qt Test:
- 转到“File ” > “New Project ” > “Test Project ”。
- 选择 Qt Test Project > “Choose ”。
- 在“Project and Test Information ”对话框中,指定项目和测试的设置。

- 在“Test case name ”中,输入测试用例的名称。
- 选择“Requires QApplication ”以将QApplication 的include语句添加到项目的
main.cpp文件中。 - 选择“GUI Application ”以创建一个 Qt 应用程序。
- 选择“Generate initialization and cleanup code ”以向测试中添加函数,测试框架将执行这些函数来初始化和清理测试。
- 在“Build system ”中,选择用于构建项目的构建系统:CMake、qmake 或 Qbs。若在使用 Qt 5 或 Qt 6.4 及更早版本进行开发时希望使用 CMake 进行构建,请选择“CMake with Qt 5 Compatibility ”。
Qt Creator 将在指定的项目目录中创建测试。编辑.cpp 文件,为测试中的每个测试函数添加私有槽。
有关创建 Qt Test 的更多信息,请参阅《创建测试》。
注意:在 扫描测试时 ,解析器仅考虑那些直接或间接包含QTest 、QtTest 或其等效文件QtTest/qtest.h ,且在构建项目时与QTestLib 链接的文件。当前不会被构建的文件将被忽略。
注意:如果 启用了General >Process arguments ,您可以并行执行测试并写入一个或多个额外的日志文件。请使用输出格式选项-o filename,format 来实现。不允许将标准输出用作filename ,因为Qt Creator 正在使用此输出通道。
Copyright © The Qt Company Ltd. and other contributors. 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.