尝试使用REQUIRED_FILES
的简短示例:
$ tree .
.
├── CMakeLists.txt
└── main.cxx
$ cat main.cxx
int main() { return 0; }
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
add_executable(a.out main.cxx)
include(CTest)
add_test(NAME a.out COMMAND a.out)
set_property(TEST a.out PROPERTY REQUIRED_FILES $<TARGET_FILE:a.out>)
一个可执行文件,不执行任何操作,并且是一个仅要求自身存在的测试。
$ mkdir build && cd build && cmake .. && ctest
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/lib64/ccache/cc
-- Check for working C compiler: /usr/lib64/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib64/ccache/c++
-- Check for working CXX compiler: /usr/lib64/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /[...]/req/build
Test project /[...]/req/build
Start 1: a.out
Could not find executable /[...]/req/build/a.out
Looked in the following places:
/[...]/req/build/a.out
/[...]/req/build/a.out
/[...]/req/build/Release/a.out
/[...]/req/build/Release/a.out
/[...]/req/build/Debug/a.out
/[...]/req/build/Debug/a.out
/[...]/req/build/MinSizeRel/a.out
/[...]/req/build/MinSizeRel/a.out
/[...]/req/build/RelWithDebInfo/a.out
/[...]/req/build/RelWithDebInfo/a.out
/[...]/req/build/Deployment/a.out
/[...]/req/build/Deployment/a.out
/[...]/req/build/Development/a.out
/[...]/req/build/Development/a.out
[...]/req/build/a.out
[...]/req/build/a.out
[...]/req/build/Release/a.out
[...]/req/build/Release/a.out
[...]/req/build/Debug/a.out
[...]/req/build/Debug/a.out
[...]/req/build/MinSizeRel/a.out
[...]/req/build/MinSizeRel/a.out
[...]/req/build/RelWithDebInfo/a.out
[...]/req/build/RelWithDebInfo/a.out
[...]/req/build/Deployment/a.out
[...]/req/build/Deployment/a.out
[...]/req/build/Development/a.out
[...]/req/build/Development/a.out
Unable to find required file: /[...]/req/build/a.out
1/1 Test #1: a.out ............................***Not Run 0.00 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.00 sec
The following tests FAILED:
1 - a.out (Not Run)
Errors while running CTest
我认为REQUIRED_FILES
的目的是避免运行此测试,因为该文件不存在,因此 not 测试失败。实际使用此属性的正确方法是什么?
答案 0 :(得分:1)
您正确使用了该属性,因为ctest并未实际运行测试。 CTest不能将测试标记为未运行,否则不会将其标记为失败,因此它将测试放置在Not Run
bin中。
至于为什么找不到所需文件的原因,则是由于未构建a.out二进制文件。您缺少脚本中的步骤:
mkdir build && cd build && cmake .. && cmake --build . && ctest
您还可以将脚本简化为: