🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 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-a`、`feature-b`和`feature-c`,还有三个长测试,名称分别是`feature-d`、`benchmark-a`和`benchmark-b`。这个示例中,我们可以用Python脚本表示这些测试,可以在其中调整休眠时间: ```python import sys import time # wait for 0.1 seconds time.sleep(0.1) # finally report success sys.exit(0) ``` ## 具体实施 以下是我们CMakeLists.txt文件内容的详细内容: 1. `CMakeLists.txt`中,定义了六个测试: ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) # project name project(recipe-09 LANGUAGES NONE) # detect python find_package(PythonInterp REQUIRED) # define tests enable_testing() add_test( NAME feature-a COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-a.py ) add_test( NAME feature-b COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-b.py ) add_test( NAME feature-c COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-c.py ) add_test( NAME feature-d COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/feature-d.py ) add_test( NAME benchmark-a COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark-a.py ) add_test( NAME benchmark-b COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark-b.py ) ``` 2. 此外,我们给较短的测试贴上`quick`的标签,给较长的测试贴上`long`的标签: ```cmake set_tests_properties( feature-a feature-b feature-c PROPERTIES LABELS "quick" ) set_tests_properties( feature-d benchmark-a benchmark-b PROPERTIES LABELS "long" ) ``` 3. 我们现在可以运行测试集了,如下: ```shell $ mkdir -p build $ cd build $ cmake .. $ ctest Start 1: feature-a 1/6 Test #1: feature-a ........................ Passed 0.11 sec Start 2: feature-b 2/6 Test #2: feature-b ........................ Passed 0.11 sec Start 3: feature-c 3/6 Test #3: feature-c ........................ Passed 0.11 sec Start 4: feature-d 4/6 Test #4: feature-d ........................ Passed 0.51 sec Start 5: benchmark-a 5/6 Test #5: benchmark-a ...................... Passed 0.51 sec Start 6: benchmark-b 6/6 Test #6: benchmark-b ...................... Passed 0.51 sec 100% tests passed, 0 tests failed out of 6 Label Time Summary: long = 1.54 sec*proc (3 tests) quick = 0.33 sec*proc (3 tests) Total Test time (real) = 1.87 sec ``` ## 工作原理 现在每个测试都有一个名称和一个标签。CMake中所有的测试都是有编号的,所以它们也带有唯一编号。定义了测试标签之后,我们现在可以运行整个集合,或者根据它们的名称(使用正则表达式)、标签或编号运行测试。 按名称运行测试(运行所有具有名称匹配功能的测试): ```shell $ ctest -R feature Start 1: feature-a 1/4 Test #1: feature-a ........................ Passed 0.11 sec Start 2: feature-b 2/4 Test #2: feature-b ........................ Passed 0.11 sec Start 3: feature-c 3/4 Test #3: feature-c ........................ Passed 0.11 sec Start 4: feature-d 4/4 Test #4: feature-d ........................ Passed 0.51 sec 100% tests passed, 0 tests failed out of 4 ``` 按照标签运行测试(运行所有的长测试): ```shell $ ctest -L long Start 4: feature-d 1/3 Test #4: feature-d ........................ Passed 0.51 sec Start 5: benchmark-a 2/3 Test #5: benchmark-a ...................... Passed 0.51 sec Start 6: benchmark-b 3/3 Test #6: benchmark-b ...................... Passed 0.51 sec 100% tests passed, 0 tests failed out of 3 ``` 根据数量运行测试(运行测试2到4)产生的结果是: ```shell $ ctest -I 2,4 Start 2: feature-b 1/3 Test #2: feature-b ........................ Passed 0.11 sec Start 3: feature-c 2/3 Test #3: feature-c ........................ Passed 0.11 sec Start 4: feature-d 3/3 Test #4: feature-d ........................ Passed 0.51 sec 100% tests passed, 0 tests failed out of 3 ``` ## 更多信息 尝试使用` $ ctest --help `,将看到有大量的选项可供用来定制测试。