使用 CMake + CTest + CDash 时,我可以有一个可选的测试夹具吗?

When using CMake + CTest + CDash can I have an optional test fixture?

我使用 CMake FIXTURES_SETUP/FIXTURES_REQUIRED 仅 运行 测试外部资源是否可用。但是,外部资源是可选的(在某些测试机器上可用,但在其他机器上不可用)所以当检查资源的装置失败时,我不想认为测试套件失败,我只是不想 运行 任何需要夹具的测试。有没有办法将测试夹具标记为 'allowed to fail'。我知道有 WILL_FAIL 但这会颠倒测试的意义,以便在通过时将其标记为失败。

没有。添加测试意味着您希望该测试通过。当该测试的先决条件失败时,CMake 会跳过该测试(实际上 运行 它)并将其计为失败,因为它没有成功。

例如

# CMakeLists.txt
cmake_minimum_required(VERSION 3.3)

project(example)

enable_testing()

add_test(NAME failIfUnavail COMMAND false)
add_test(NAME dependentTest1 COMMAND true)
add_test(NAME dependentTest2 COMMAND true)
add_test(NAME cleaner COMMAND true)

set_tests_properties(failIfUnavail PROPERTIES FIXTURES_SETUP example_case)
set_tests_properties(dependentTest1 dependentTest2 PROPERTIES FIXTURES_REQUIRED example_case)
set_tests_properties(cleaner PROPERTIES FIXTURES_CLEANUP example_case)
$ cmake -H. -Bbuild
-- The C compiler identification is GNU 8.2.0
-- The CXX compiler identification is GNU 8.2.0
-- Check for working C compiler: /bin/gcc
-- Check for working C compiler: /bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /bin/g++
-- Check for working CXX compiler: /bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build
$ cmake --build build/ --target test
Running tests...
Test project /home/pamini/cmake_test1/build
    Start 1: failIfUnavail
1/4 Test #1: failIfUnavail ....................***Failed    0.00 sec
    Start 2: dependentTest1
Failed test dependencies: failIfUnavail
2/4 Test #2: dependentTest1 ...................***Not Run   0.00 sec
    Start 3: dependentTest2
Failed test dependencies: failIfUnavail
3/4 Test #3: dependentTest2 ...................***Not Run   0.00 sec
    Start 4: cleaner
4/4 Test #4: cleaner ..........................   Passed    0.00 sec

25% tests passed, 3 tests failed out of 4

Total Test time (real) =   0.02 sec

The following tests FAILED:
          1 - failIfUnavail (Failed)
          2 - dependentTest1 (Not Run)
          3 - dependentTest2 (Not Run)
Errors while running CTest
gmake: *** [test] Error 8

您可以做的是:

  1. 如果资源的可用性在配置时已知,则检查并仅在您希望测试通过时添加测试。
  2. 如果资源的可用性在配置时未知,则修改测试本身以解决这种不确定性。例如,单元测试通过使用模仿它们所依赖的资源行为的模拟对象来避免这个问题。