创建 Qt Test

Qt Creator 集成了 Qt Test框架,用于对 Qt 应用程序和库进行单元测试。

创建 Qt 测试:

  1. 转到File >New Project >Test Project
  2. 选择 Qt Test Project>Choose
  3. Project and Test Information 对话框中,指定项目和测试的设置。

    Qt Test 项目向导中的项目和测试信息对话框

  4. Test case name 中,输入测试用例的名称。
  5. 选择Requires QApplication ,将QApplication 的 include 语句添加到项目的 main.cpp 文件中。
  6. 选择GUI Application ,创建 Qt XML 应用程序。
  7. 选择Generate initialization and cleanup code ,向测试中添加测试框架执行的函数,以初始化和清理测试。
  8. Build system 中,选择用于构建项目的构建系统:CMake、qmake 或 Qbs。使用 Qt 5 或 Qt 6.4 或更早版本开发时,要使用 CMake 构建,请选择CMake for Qt 5 and Qt 6

Qt Creator 在指定的项目目录中创建测试。编辑 .cpp 文件,为测试中的每个测试函数添加私有槽。

有关创建 Qt 测试的更多信息,请参阅创建测试

注意: 在扫描测试时,解析器只考虑直接或间接包含QTestQtTest 或其等价物QtTest/qtest.h 并在构建项目时与QTestLib 链接的文件。目前不会构建的文件将被忽略。

另请参阅 "如何:测试"、"选择构建系统"、"测试"和 "测试结果"。

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.