How to make ctest run test executables in a transient/temporary directory
我每次运行
假设我有一个测试可执行文件
我想通过要求cmake / ctest在其自己的临时目录中运行每个测试(在此示例中为一个测试)来实现这一目标。
我已经在线搜索了解决方案,并且已经阅读了
这是触发故障的最小,可重复的方法。一个定义测试可执行文件的源文件
1 2 3 4 5 6 7 | # CMakeLists.txt cmake_minimum_required (VERSION 2.8) project (CMakeHelloWorld) enable_testing() add_executable (mytest mytest.cpp) add_test( testname mytest) |
和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // mytest.cpp #include <sys/stat.h> #include <unistd.h> #include <string> #include <fstream> inline bool exists (const std::string& name) { std::ifstream f(name.c_str()); return f.good(); } int main() { assert(exists("foo.txt") == false); std::ofstream outfile ("foo.txt"); outfile.close(); } |
产生故障的命令系列
1 2 3 4 5 6 | $ mkdir build $ cd build $ cmake .. $ make $ make test $ make test |
这将给出
1 2 3 4 5 6 7 8 9 10 11 12 13 | Running tests... Test project /path/to/project Start 1: testname 1/1 Test #1: testname .........................***Exception: Other 0.25 sec 0% tests passed, 1 tests failed out of 1 Total Test time (real) = 0.26 sec The following tests FAILED: 1 - testname (OTHER_FAULT) Errors while running CTest make: *** [test] Error 8 |
通常,测试框架提供某种预测试(设置)和后测试(清理)任务。 CTest也是如此。
将以下
1 2 |
对于更复杂的任务,您可能需要创建一个自定义脚本,但这是调用它的方式。