Is it possible in CMake to set properties of tests in a different directory scope than the one the test is defined in?

huangapple go评论66阅读模式
英文:

Is it possible in CMake to set properties of tests in a different directory scope than the one the test is defined in?

问题

For more structured test execution with "ctest -L," I want to set some labels on my unit tests. The labels I want to assign to a test are not yet clear at the time I declare the test. CMake seems to fail finding the test when not in the same directory scope, however with the same semantics it can modify properties of a target.

Here is my setup:

.
├── CMakeLists.txt
└── a
    ├── CMakeLists.txt
    ├── a.cpp
    └── test_a.cpp

Where:
./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 output is:

-- 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!

Using set_tests_properties(mytest_a PROPERTIES LABELS "bla") in a/CMakeLists.txt works, but not in the parent scope. Is this behavior intended? If so, why? Is there a workaround?

英文:

For more structured test execution with "ctest -L" I want to set some labels on my unit tests. The labels I want to assign to a test are not yet clear at the time I declare the test. CMake seems to fail finding the test when not in the same directory scope, however with the same semantics it can modify properties of a target.

Here is my setup:

.
├── CMakeLists.txt
└── a
    ├── CMakeLists.txt
    ├── a.cpp
    └── test_a.cpp

Where:
./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 &quot;bla&quot;) # &lt;-- works
set_tests_properties(mytest_a PROPERTIES LABELS &quot;bla&quot;) # &lt;-- 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 output is:

-- 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!

using set_tests_properties(mytest_a PROPERTIES LABELS &quot;bla&quot;) in a/CMakeLists.txt works, but not in the parent scope. Is this behavior intended? if so why? Is there a workaround?

答案1

得分: 0

如果你在Google中搜索"set_property给定不存在的TEST名称",你应该能够找到(在discourse.cmake.org上) 无法在子目录中设置测试的属性。简而言之,测试名称不需要在目录范围内唯一(目前未记录在案 - 至少在add_test的文档中未记录),因此你观察到的行为目前是有意的(对于set_tests_propertiesset_property(TEST ...)都是如此)。当被询问时,维护者们并不知道有任何解决方法,除了编写一个包装函数来添加属性,并在与add_test定义测试的目录范围内调用该函数。与此行为相关的问题已创建:https://gitlab.kitware.com/cmake/cmake/-/issues/22813。

英文:

If you google "&quot;set_property given TEST names that do not exist&quot;", you should should be able to find (on discourse.cmake.org) Cannot set property of a test in a subdirectory. The TL;DR is that test names are not required to be unique across directory scopes (currently not documented- at least- not in the docs for add_test), so the behaviour you're observing is currently intended (both for set_tests_properties and set_property(TEST ...)). When asked, the maintainers were not aware of any workarounds, aside from writing a wrapper function that adds the property, and calling the function in the same directory scope as where the test is defined by add_test. An issue ticket was created in relation to this behaviour: https://gitlab.kitware.com/cmake/cmake/-/issues/22813.

huangapple
  • 本文由 发表于 2023年5月17日 16:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定