0

我有一个可执行文件,其中包含用QTest编写的多个测试。
可执行文件正在使用 注册add_tests(NAME test_name COMMAND test_executable)。显然,测试将在CTest中作为一个处理,因此如果数百个测试中的一个失败,将很难找到问题。添加带有测试的新可执行文件时情况会更糟。

是否可以像 GTest 实现一样单独注册测试?QTest 测试是否有GTEST_ADD_TESTSGTEST_DISCOVER_TESTS CMake 函数的替代方法?

4

2 回答 2

0

使用ADD_CUSTOM_TARGET运行您测试。

add_custom_target(run_test qtest_executable_target)
cmake <dir>
cmake --build <dir> -t run_test
于 2021-08-23T20:07:26.780 回答
0

只是使用QTest我没有找到解决方案,但尝试了另一种可行的方法。

我将测试框架更改为GTest。但是,测试应用程序仍然与QTest库链接,以访问模拟 GUI 交互等功能。

gtest_main模块的默认main函数可能不兼容,因为它需要为 Qt 的许多功能初始化QApplication。在gtest_main.cc源文件中有一个基于GTest的main实现。

#include <cstdio>
#include <gtest/gtest.h>

#include <QApplication>

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    printf("Running main() from %s\n", __FILE__);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

After testing, I didn't find any problem as well as using GTEST_ADD_TESTS or GTEST_DISCOVER_TESTS CMake functions.

于 2021-08-26T14:49:08.900 回答