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

ctest: Disable a set of labeled tests by default - but run them when explicitly targeted

我想在默认情况下禁用一组测试,但能够 运行 在明确针对它们时。

例如假设我有一个项目,其中包含许多通过 add_test(NAME SomeNameHere COMMAND SomeCommandHere) 命令添加的快速 运行 单元测试。并且进一步假设还有一堆long-运行ning集成测试应该排除在默认ctest运行之外,但是应该可以运行全部明确定位时使用单个命令进行集成测试。

举个具体的例子:

这是我实现此目标的尝试。

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

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

我探索了其他方法,例如

  • 不禁用集成测试。然后,当用户想要 运行 快速 运行ning 测试时,他们需要指定 ctest --label-exclude my_integration_tests。这不是很好。这让每个人都难以记住这一点。我希望使用最多的选项(运行仅快速-运行ning 测试)通过 ctest.
  • 运行nable
  • 不禁用集成测试,但向 add_test 调用提供参数 CONFIGURATIONS my_integration_tests。但我认为那样我在语义上误用了 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 并且如果集成测试应该 return 成功'不是 运行):

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