0

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

4

1 回答 1

1

不会。当您添加测试时,意味着您希望该测试通过。当该测试的先决条件失败时,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. 如果在配置时不知道资源的可用性,则修改测试本身以解决这种不确定性。例如,单元测试通过使用模仿它们所依赖的资源行为的模拟对象来避免这个问题。
于 2019-03-13T19:08:29.430 回答