本页内容

第 1 章:编写单元测试

本章将演示如何编写一个简单的单元测试,以及如何将测试用例作为独立可执行程序运行。

编写测试

假设你想测试我们的QString 类的行为。首先,你需要一个包含测试函数的类。该类必须继承自QObject :

#include <QTest>

class TestQString: public QObject
{
    Q_OBJECT
private slots:
    void toUpper();
};

注意:你 需要包含QTest 头文件,并将测试函数声明为私有槽,以便测试框架能够找到并执行它们。

然后,你需要实现测试函数本身。实现代码可能如下所示:

void TestQString::toUpper()
{
    QString str = "Hello";
    QVERIFY(str.toUpper() == "HELLO");
}

QVERIFY() 宏会求值其作为参数传递的表达式。如果表达式求值结果为 true,则测试函数继续执行;否则,会将描述失败原因的消息追加到测试日志中,并且测试函数停止执行。

但如果你希望在测试日志中输出更详细的记录,应改用 `QCOMPARE()` 宏:

void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}

如果两个字符串不相同,则会将两个字符串的内容都追加到测试日志中,从而立即可知比较失败的原因。

准备独立可执行文件

最后,要使我们的测试用例成为一个独立的可执行文件,需要添加以下两行代码:

QTEST_MAIN(TestQString)
#include "testqstring.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(tutorial1 LANGUAGES CXX)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)

qt_standard_project_setup()

qt_add_executable(tutorial1
    testqstring.cpp
)

set_target_properties(tutorial1 PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)

target_link_libraries(tutorial1 PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Test
    Qt6::Widgets
)

install(TARGETS tutorial1
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET tutorial1
    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 = testqstring.cpp

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

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

qmake
make

注意:若 您使用的是 Windows 系统,请将make 替换为nmake 或您所使用的其他构建工具。

运行可执行文件

运行生成的可执行文件应显示以下输出:

********* Start testing of TestQString *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS   : TestQString::initTestCase()
PASS   : TestQString::toUpper()
PASS   : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********

恭喜!您刚刚使用Qt Test 框架编写并执行了您的第一个单元测试。

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