我想默认禁用一组测试,但能够在明确定位时运行它们。
例如。假设我有一个项目,其中包含许多通过 add_test(NAME SomeNameHere COMMAND SomeCommandHere)
添加的快速运行的单元测试。命令。并进一步假设还有一堆长时间运行的集成测试应该从默认 ctest
中排除。运行,但是当明确定位时,应该可以使用单个命令运行所有集成测试。
作为一个具体的例子:
TestThatShouldAlwaysRun
- 我希望在用户运行时运行 $ ctest
FooIntegrationTest
- 我希望只有当用户运行时才运行 $ ctest --some-option-here integration
BarIntegrationTest
- 我希望只有当用户运行时才运行 $ ctest --some-option-here integration
这是我实现这一目标的尝试。
cmake_minimum_required (VERSION 3.1)
project (HOW_TO_RUN_DISABLED_TESTS NONE)
enable_testing()
# Add a fast running test
# This test should be run when a user runs: 'ctest'
add_test(NAME TestThatShouldAlwaysRun COMMAND echo 'I am always enabled')
# Add a long-running integration test
# This test should be run when a user runs: 'ctest --label-regex my_integration_tests'
add_test(NAME FooIntegrationTest COMMAND echo 'Foo integration test')
set_tests_properties(FooIntegrationTest PROPERTIES DISABLED True)
set_tests_properties(FooIntegrationTest PROPERTIES LABELS my_integration_tests)
# Add another long-running integration test
# This test should be run when a user runs: 'ctest --label-regex my_integration_tests'
add_test(NAME BarIntegrationTest COMMAND echo 'Bar integration test')
set_tests_properties(BarIntegrationTest PROPERTIES DISABLED True)
set_tests_properties(BarIntegrationTest PROPERTIES LABELS my_integration_tests)
现在,让我们通过
$ mkdir build
$ cd build/
$ cmake ..
$ ctest -V
正如我们在输出中看到的(发布在下面)
ctest
确实排除了集成测试(太棒了!)。1: Test command: /bin/echo "'I" "am" "always" "enabled'"
1: Test timeout computed to be: 10000000
1: 'I am always enabled'
1/3 Test #1: TestThatShouldAlwaysRun .......... Passed 0.00 sec
test 2
Start 2: FooIntegrationTest
2/3 Test #2: FooIntegrationTest ...............***Not Run (Disabled) 0.00 sec
test 3
Start 3: BarIntegrationTest
3/3 Test #3: BarIntegrationTest ...............***Not Run (Disabled) 0.00 sec
100% tests passed, 0 tests failed out of 1
Label Time Summary:
my_integration_tests = 0.00 sec*proc (2 tests)
Total Test time (real) = 0.05 sec
The following tests did not run:
2 - FooIntegrationTest (Disabled)
3 - BarIntegrationTest (Disabled)
但是,我现在不知道如何通过
ctest
, 运行标有 my_integration_tests
的测试.ctest --label-regex my_integration_tests
Test project /path/to/code/example_repo/build
Start 2: FooIntegrationTest
1/2 Test #2: FooIntegrationTest ...............***Not Run (Disabled) 0.00 sec
Start 3: BarIntegrationTest
2/2 Test #3: BarIntegrationTest ...............***Not Run (Disabled) 0.00 sec
No tests were found!!!
有没有办法在明确定位时运行禁用的测试?
我已经探索了其他方式,比如
ctest --label-exclude my_integration_tests
.这不是很好。这让每个人都负担不起要记住这一点。我希望使用最多的选项(仅运行快速运行的测试)可以通过 ctest
运行。 . CONFIGURATIONS my_integration_tests
到 add_test
称呼。但我认为这样我在语义上误用了 CONFIGURATIONS
.此外,这不允许我仅在有针对性的情况下运行集成测试。 add_custom_target
添加集成测试.同样在这里,我认为我在滥用 API。 add_custom_target
用于自定义目标,而不是作为运行测试的一种方式,对吗? 最佳答案
我相信更简单的方法是通过 cmake 配置选项。
# cat CMakeLists.txt
cmake_minimum_required (VERSION 3.1)
project (HOW_TO_RUN_DISABLED_TESTS NONE)
enable_testing()
add_test(NAME TestThatShouldAlwaysRun COMMAND echo 'I am always enabled')
if(BUILD_INTEGRATION_TESTING)
add_test(NAME FooIntegrationTest COMMAND echo 'Foo integration test')
add_test(NAME BarIntegrationTest COMMAND echo 'Bar integration test')
endif()
这很简单,也更灵活,即。允许为这些测试添加要构建的测试相关目标(和设置(和要使用的 ctest 固定装置))。 (我通常在项目中看到名为
SOMETHING_BUILD_TESTING
的变量,所以它类似于 BUILD_TESTING
cmake 变量。)我的另一个想法是使用环境变量并将测试包装在脚本中
SKIP_RETURN_CODE
(或者甚至没有 SKIP_RETURN_CODE
并且如果不应该运行集成测试就返回成功):add_test(NAME FooIntegrationTest
COMMAND sh -c "if [ \"\${RUN_INTEGRATION:-}\" ]; then echo 'Foo integration test'; else exit 127; fi"
VERBATIM)
set_tests_properties(FooIntegrationTest PROPERTIES
SKIP_RETURN_CODE 127)
然后:
$ ctest -V
....
2: Test command: /usr/bin/sh "-c" "if [ "${RUN_INTEGRATION:-}" ]; then echo 'Foo integration test'; else exit 127; fi" "VERBATIM"
2: Test timeout computed to be: 10000000
2/3 Test #2: FooIntegrationTest ...............***Skipped 0.01 sec
....
The following tests did not run:
2 - FooIntegrationTest (Skipped)
但:
$ RUN_INTEGRATION=TRUE ctest -V
...
2: Test command: /usr/bin/sh "-c" "if [ "${RUN_INTEGRATION:-}" ]; then echo 'Foo integration test'; else exit 127; fi" "VERBATIM"
2: Test timeout computed to be: 10000000
2: Foo integration test
2/3 Test #2: FooIntegrationTest ............... Passed 0.01 sec
...
关于cmake - ctest:默认禁用一组标记测试 - 但在明确定位时运行它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61572034/