ctest:默认禁用一组标记测试 - 但在明确定位时运行它们

Mat*_*att 5 cmake ctest

我想默认禁用一组测试,但能够在明确定位时运行它们。

例如,假设我有一个项目,其中包含许多通过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)
Run Code Online (Sandbox Code Playgroud)

现在,让我们通过

$ mkdir build
$ cd build/
$ cmake ..
$ ctest -V
Run Code Online (Sandbox Code Playgroud)

正如我们在输出中看到的(发布在下面)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)
Run Code Online (Sandbox Code Playgroud)

但是,我不知道我现在如何通过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!!!
Run Code Online (Sandbox Code Playgroud)

有没有办法在明确定位时运行禁用的测试?

我已经探索了其他方式,比如

  • 不禁用集成测试。然后,当用户想要运行快速运行的测试时,他们需要指定ctest --label-exclude my_integration_tests. 这不是很好。这让每个人都负担不起要记住这一点。我希望使用最多的选项(仅运行快速运行的测试)可以通过ctest.
  • 不是去功能集成测试,但提供的参数CONFIGURATIONS my_integration_testsadd_test调用。但我认为这样我在语义上误用了CONFIGURATIONS. 此外,这不允许我在有针对性的情况下运行集成测试。
  • 通过add_custom_target. 同样在这里,我认为我在滥用 API。 add_custom_target用于自定义目标,而不是作为运行测试的一种方式,对吗?

Kam*_*Cuk 4

我相信更简单的方法是通过 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()
Run Code Online (Sandbox Code Playgroud)

这很简单,也更灵活,即。允许为这些测试添加要构建(和设置(以及要使用的 ctest 固定装置))的测试相关目标。(我通常在项目中看到这样命名的变量,SOMETHING_BUILD_TESTING因此它类似于BUILD_TESTINGcmake 变量。)

我的另一个想法是使用环境变量并将测试包装在脚本中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)
Run Code Online (Sandbox Code Playgroud)

然后:

$ 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 Code Online (Sandbox Code Playgroud)

但:

$ 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
...
Run Code Online (Sandbox Code Playgroud)