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

时间:2018-08-22 10:15:54

标签: cmake ctest cdash

我正在使用CMake FIXTURES_SETUP / FIXTURES_REQUIRED仅在有外部资源可用时运行测试。但是,外部资源是可选的(在某些测试机上可用,但在其他测试机上不可用),因此当检查资源的设备失败时,我不想考虑测试套件已经失败,我只是不想运行任何测试需要固定装置。有没有一种方法可以将测试夹具标记为“允许失败”。我知道这里有WILL_FAIL,但这会颠覆测试的意义,以便在通过测试时将其标记为失败。

1 个答案:

答案 0 :(得分:0)

不。添加测试意味着您希望该测试通过。当该测试的先决条件失败时,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. 如果在配置时不知道资源的可用性,请修改测试本身以解决这种不确定性。例如,单元测试通过使用模拟对象来模仿该对象所依赖的资源的行为,从而避免了这一问题。