调用Unittest++;从cmake创建的makefile
我最近开始学习cmake,遇到了一个小问题。我从生成的makefile中编译了可执行文件和单元测试,没有任何问题。如果我在build目录中运行调用Unittest++;从cmake创建的makefile,cmake,Cmake,我最近开始学习cmake,遇到了一个小问题。我从生成的makefile中编译了可执行文件和单元测试,没有任何问题。如果我在build目录中运行/test,那么在UnitTest++中创建的测试将按预期运行并完成,并打印结果。有没有办法让maketest简单地运行测试可执行文件,而不是在ctest框架内运行它,或者我应该用另一种方法来实现这一点 下面是我的代码的一个最简单的工作示例: src/main/main.c是一个简单的空main函数 src/test/testMain.cpp: #incl
/test
,那么在UnitTest++中创建的测试将按预期运行并完成,并打印结果。有没有办法让maketest
简单地运行测试可执行文件,而不是在ctest框架内运行它,或者我应该用另一种方法来实现这一点
下面是我的代码的一个最简单的工作示例:
src/main/main.c是一个简单的空main函数
src/test/testMain.cpp:
#include <UnitTest++/UnitTest++.h>
TEST(FailSpect)
{
CHECK(false);
}
int main()
{
UnitTest::RunAllTests();
}
进行测试
输出:
Running tests...
Start processing tests
Test project /myProjectDir/build
1/ 1 Testing testExe Passed
100% tests passed, 0 tests failed out of 1
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
/testSuite
输出:
Running tests...
Start processing tests
Test project /myProjectDir/build
1/ 1 Testing testExe Passed
100% tests passed, 0 tests failed out of 1
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
我已经决定了怎么做。首先,删除以下行:
enable_testing()
及
并将其替换为以下行:
add_custom_target(test ./testExe
DEPENDS ./testExe)
在CMakeLists.txt文件的末尾。现在make(all)
构建测试和主程序。如果所有内容都已生成,则make test
只需检查测试是否已生成并运行,即可生成:
[100%] Built target testExe
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
[100%] Built target test
如果测试过期(例如,在makeclean
之后),则maketest
将产生:
[100%] Building CXX object CMakeFiles/testExe.dir/src/test/testMain.cpp.o
Linking CXX executable testExe
[100%] Built target testExe
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
[100%] Built target test