对于使用“ctest-L”执行的更结构化的测试,我想在单元测试上设置一些标签。在声明测试时,要分配给测试的标签还不清楚。当不在同一目录范围内时,CMake似乎无法找到测试,但是使用相同的语义,它可以修改目标的属性。
以下是我的设置:
.
├── CMakeLists.txt
└── a
├── CMakeLists.txt
├── a.cpp
└── test_a.cpp
其中:./CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(cmaketest LANGUAGES CXX)
enable_testing()
add_subdirectory(a)
# modify target and test properties
set_target_properties(a PROPERTIES LABELS "bla") # <-- works
set_tests_properties(mytest_a PROPERTIES LABELS "bla") # <-- fails
./a/CMakeLists.txt:
add_library(a src/a.cpp)
# add tests for my
add_executable(test_a test/test_a.cpp)
target_link_libraries(test_a a)
add_test(NAME mytest_a COMMAND ./test_a)
CMake输出为:
-- The CXX compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:9 (set_tests_properties):
set_tests_properties Can not find test to add properties to: mytest_a
-- Configuring incomplete, errors occurred!
在a/CMakeLists.txt中使用set_tests_properties(mytest_a PROPERTIES LABELS "bla")
可以工作,但不能在父作用域中工作。这种行为是故意的吗?如果是,为什么?有没有变通办法?
1条答案
按热度按时间pb3skfrl1#
如果你用谷歌搜索“
"set_property given TEST names that do not exist"
”,你应该可以找到(on discourse.cmake.org) Cannot set property of a test in a subdirectory。TL;DR是不要求测试名称在目录范围内是唯一的(目前没有记录-至少-在add_test
的文档中没有),因此您正在观察的行为是当前预期的(对于set_tests_properties
和set_property(TEST ...)
)。When asked, the maintainers were not aware of any workarounds,除了编写一个添加属性的 Package 器函数,并在与add_test
定义测试的目录范围相同的目录范围内调用该函数。已创建与此行为相关的问题单:https://gitlab.kitware.com/cmake/cmake/-/issues/22813。