4.9 运行测试子集

NOTE:此示例代码可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-04/recipe-09 中找到。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

前面的示例中,我们学习了如何在CMake的帮助下并行运行测试,并讨论了从最长的测试开始是最高效的。虽然,这种策略将总测试时间最小化,但是在特定特性的代码开发期间,或者在调试期间,我们可能不希望运行整个测试集。对于调试和代码开发,我们只需要能够运行选定的测试子集。在本示例中,我们将实现这一策略。

准备工作

在这个例子中,我们假设总共有六个测试:前三个测试比较短,名称分别为feature-afeature-bfeature-c,还有三个长测试,名称分别是feature-dbenchmark-abenchmark-b。这个示例中,我们可以用Python脚本表示这些测试,可以在其中调整休眠时间:

  1. import sys
  2. import time
  3. # wait for 0.1 seconds
  4. time.sleep(0.1)
  5. # finally report success
  6. sys.exit(0)

具体实施

以下是我们CMakeLists.txt文件内容的详细内容:

  1. CMakeLists.txt中,定义了六个测试:

    1. cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
    2. # project name
    3. project(recipe-09 LANGUAGES NONE)
    4. # detect python
    5. find_package(PythonInterp REQUIRED)
    6. # define tests
    7. enable_testing()
    8. add_test(
    9. NAME feature-a
    10. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-a.py
    11. )
    12. add_test(
    13. NAME feature-b
    14. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-b.py
    15. )
    16. add_test(
    17. NAME feature-c
    18. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-c.py
    19. )
    20. add_test(
    21. NAME feature-d
    22. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-d.py
    23. )
    24. add_test(
    25. NAME benchmark-a
    26. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark-a.py
    27. )
    28. add_test(
    29. NAME benchmark-b
    30. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark-b.py
    31. )
  2. 此外,我们给较短的测试贴上quick的标签,给较长的测试贴上long的标签:

    1. set_tests_properties(
    2. feature-a
    3. feature-b
    4. feature-c
    5. PROPERTIES
    6. LABELS "quick"
    7. )
    8. set_tests_properties(
    9. feature-d
    10. benchmark-a
    11. benchmark-b
    12. PROPERTIES
    13. LABELS "long"
    14. )
  3. 我们现在可以运行测试集了,如下:

    1. $ mkdir -p build
    2. $ cd build
    3. $ cmake ..
    4. $ ctest
    5. Start 1: feature-a
    6. 1/6 Test #1: feature-a ........................ Passed 0.11 sec
    7. Start 2: feature-b
    8. 2/6 Test #2: feature-b ........................ Passed 0.11 sec
    9. Start 3: feature-c
    10. 3/6 Test #3: feature-c ........................ Passed 0.11 sec
    11. Start 4: feature-d
    12. 4/6 Test #4: feature-d ........................ Passed 0.51 sec
    13. Start 5: benchmark-a
    14. 5/6 Test #5: benchmark-a ...................... Passed 0.51 sec
    15. Start 6: benchmark-b
    16. 6/6 Test #6: benchmark-b ...................... Passed 0.51 sec
    17. 100% tests passed, 0 tests failed out of 6
    18. Label Time Summary:
    19. long = 1.54 sec*proc (3 tests)
    20. quick = 0.33 sec*proc (3 tests)
    21. Total Test time (real) = 1.87 sec

工作原理

现在每个测试都有一个名称和一个标签。CMake中所有的测试都是有编号的,所以它们也带有唯一编号。定义了测试标签之后,我们现在可以运行整个集合,或者根据它们的名称(使用正则表达式)、标签或编号运行测试。

按名称运行测试(运行所有具有名称匹配功能的测试):

  1. $ ctest -R feature
  2. Start 1: feature-a
  3. 1/4 Test #1: feature-a ........................ Passed 0.11 sec
  4. Start 2: feature-b
  5. 2/4 Test #2: feature-b ........................ Passed 0.11 sec
  6. Start 3: feature-c
  7. 3/4 Test #3: feature-c ........................ Passed 0.11 sec
  8. Start 4: feature-d
  9. 4/4 Test #4: feature-d ........................ Passed 0.51 sec
  10. 100% tests passed, 0 tests failed out of 4

按照标签运行测试(运行所有的长测试):

  1. $ ctest -L long
  2. Start 4: feature-d
  3. 1/3 Test #4: feature-d ........................ Passed 0.51 sec
  4. Start 5: benchmark-a
  5. 2/3 Test #5: benchmark-a ...................... Passed 0.51 sec
  6. Start 6: benchmark-b
  7. 3/3 Test #6: benchmark-b ...................... Passed 0.51 sec
  8. 100% tests passed, 0 tests failed out of 3

根据数量运行测试(运行测试2到4)产生的结果是:

  1. $ ctest -I 2,4
  2. Start 2: feature-b
  3. 1/3 Test #2: feature-b ........................ Passed 0.11 sec
  4. Start 3: feature-c
  5. 2/3 Test #3: feature-c ........................ Passed 0.11 sec
  6. Start 4: feature-d
  7. 3/3 Test #4: feature-d ........................ Passed 0.51 sec
  8. 100% tests passed, 0 tests failed out of 3

更多信息

尝试使用$ ctest --help,将看到有大量的选项可供用来定制测试。