|
| 1 | +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying file Copyright.txt or |
| 2 | +# https://cmake.org/licensing for details. |
| 3 | + |
| 4 | +#[=======================================================================[.rst: |
| 5 | +doctest |
| 6 | +----- |
| 7 | +
|
| 8 | +This module defines a function to help use the doctest test framework. |
| 9 | +
|
| 10 | +The :command:`doctest_discover_tests` discovers tests by asking the compiled test |
| 11 | +executable to enumerate its tests. This does not require CMake to be re-run |
| 12 | +when tests change. However, it may not work in a cross-compiling environment, |
| 13 | +and setting test properties is less convenient. |
| 14 | +
|
| 15 | +This command is intended to replace use of :command:`add_test` to register |
| 16 | +tests, and will create a separate CTest test for each doctest test case. Note |
| 17 | +that this is in some cases less efficient, as common set-up and tear-down logic |
| 18 | +cannot be shared by multiple test cases executing in the same instance. |
| 19 | +However, it provides more fine-grained pass/fail information to CTest, which is |
| 20 | +usually considered as more beneficial. By default, the CTest test name is the |
| 21 | +same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``. |
| 22 | +
|
| 23 | +.. command:: doctest_discover_tests |
| 24 | +
|
| 25 | + Automatically add tests with CTest by querying the compiled test executable |
| 26 | + for available tests:: |
| 27 | +
|
| 28 | + doctest_discover_tests(target |
| 29 | + [TEST_SPEC arg1...] |
| 30 | + [EXTRA_ARGS arg1...] |
| 31 | + [WORKING_DIRECTORY dir] |
| 32 | + [TEST_PREFIX prefix] |
| 33 | + [TEST_SUFFIX suffix] |
| 34 | + [PROPERTIES name1 value1...] |
| 35 | + [ADD_LABELS value] |
| 36 | + [TEST_LIST var] |
| 37 | + [JUNIT_OUTPUT_DIR dir] |
| 38 | + ) |
| 39 | +
|
| 40 | + ``doctest_discover_tests`` sets up a post-build command on the test executable |
| 41 | + that generates the list of tests by parsing the output from running the test |
| 42 | + with the ``--list-test-cases`` argument. This ensures that the full |
| 43 | + list of tests is obtained. Since test discovery occurs at build time, it is |
| 44 | + not necessary to re-run CMake when the list of tests changes. |
| 45 | + However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set |
| 46 | + in order to function in a cross-compiling environment. |
| 47 | +
|
| 48 | + Additionally, setting properties on tests is somewhat less convenient, since |
| 49 | + the tests are not available at CMake time. Additional test properties may be |
| 50 | + assigned to the set of tests as a whole using the ``PROPERTIES`` option. If |
| 51 | + more fine-grained test control is needed, custom content may be provided |
| 52 | + through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES` |
| 53 | + directory property. The set of discovered tests is made accessible to such a |
| 54 | + script via the ``<target>_TESTS`` variable. |
| 55 | +
|
| 56 | + The options are: |
| 57 | +
|
| 58 | + ``target`` |
| 59 | + Specifies the doctest executable, which must be a known CMake executable |
| 60 | + target. CMake will substitute the location of the built executable when |
| 61 | + running the test. |
| 62 | +
|
| 63 | + ``TEST_SPEC arg1...`` |
| 64 | + Specifies test cases, wildcarded test cases, tags and tag expressions to |
| 65 | + pass to the doctest executable with the ``--list-test-cases`` argument. |
| 66 | +
|
| 67 | + ``EXTRA_ARGS arg1...`` |
| 68 | + Any extra arguments to pass on the command line to each test case. |
| 69 | +
|
| 70 | + ``WORKING_DIRECTORY dir`` |
| 71 | + Specifies the directory in which to run the discovered test cases. If this |
| 72 | + option is not provided, the current binary directory is used. |
| 73 | +
|
| 74 | + ``TEST_PREFIX prefix`` |
| 75 | + Specifies a ``prefix`` to be prepended to the name of each discovered test |
| 76 | + case. This can be useful when the same test executable is being used in |
| 77 | + multiple calls to ``doctest_discover_tests()`` but with different |
| 78 | + ``TEST_SPEC`` or ``EXTRA_ARGS``. |
| 79 | +
|
| 80 | + ``TEST_SUFFIX suffix`` |
| 81 | + Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of |
| 82 | + every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may |
| 83 | + be specified. |
| 84 | +
|
| 85 | + ``PROPERTIES name1 value1...`` |
| 86 | + Specifies additional properties to be set on all tests discovered by this |
| 87 | + invocation of ``doctest_discover_tests``. |
| 88 | +
|
| 89 | + ``ADD_LABELS value`` |
| 90 | + Specifies if the test labels should be set automatically. |
| 91 | +
|
| 92 | + ``TEST_LIST var`` |
| 93 | + Make the list of tests available in the variable ``var``, rather than the |
| 94 | + default ``<target>_TESTS``. This can be useful when the same test |
| 95 | + executable is being used in multiple calls to ``doctest_discover_tests()``. |
| 96 | + Note that this variable is only available in CTest. |
| 97 | +
|
| 98 | + ``JUNIT_OUTPUT_DIR dir`` |
| 99 | + If specified, the parameter is passed along with ``--reporters=junit`` |
| 100 | + and ``--out=`` to the test executable. The actual file name is the same |
| 101 | + as the test target, including prefix and suffix. This should be used |
| 102 | + instead of EXTRA_ARGS to avoid race conditions writing the XML result |
| 103 | + output when using parallel test execution. |
| 104 | +
|
| 105 | +#]=======================================================================] |
| 106 | + |
| 107 | +# ------------------------------------------------------------------------------ |
| 108 | +function(doctest_discover_tests TARGET) |
| 109 | + cmake_parse_arguments( |
| 110 | + "" "" "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;JUNIT_OUTPUT_DIR" |
| 111 | + "TEST_SPEC;EXTRA_ARGS;PROPERTIES;ADD_LABELS" ${ARGN} |
| 112 | + ) |
| 113 | + |
| 114 | + if(NOT _WORKING_DIRECTORY) |
| 115 | + set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") |
| 116 | + endif() |
| 117 | + if(NOT _TEST_LIST) |
| 118 | + set(_TEST_LIST ${TARGET}_TESTS) |
| 119 | + endif() |
| 120 | + |
| 121 | + # Generate a unique name based on the extra arguments |
| 122 | + string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS}") |
| 123 | + string(SUBSTRING ${args_hash} 0 7 args_hash) |
| 124 | + |
| 125 | + # Define rule to generate test list for aforementioned test executable |
| 126 | + set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake") |
| 127 | + set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake") |
| 128 | + get_property( |
| 129 | + crosscompiling_emulator |
| 130 | + TARGET ${TARGET} |
| 131 | + PROPERTY CROSSCOMPILING_EMULATOR |
| 132 | + ) |
| 133 | + add_custom_command( |
| 134 | + TARGET ${TARGET} |
| 135 | + POST_BUILD |
| 136 | + BYPRODUCTS "${ctest_tests_file}" |
| 137 | + COMMAND |
| 138 | + "${CMAKE_COMMAND}" -D "TEST_TARGET=${TARGET}" -D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>" -D |
| 139 | + "TEST_EXECUTOR=${crosscompiling_emulator}" -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}" -D |
| 140 | + "TEST_SPEC=${_TEST_SPEC}" -D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}" -D |
| 141 | + "TEST_PROPERTIES=${_PROPERTIES}" -D "TEST_ADD_LABELS=${_ADD_LABELS}" -D |
| 142 | + "TEST_PREFIX=${_TEST_PREFIX}" -D "TEST_SUFFIX=${_TEST_SUFFIX}" -D "TEST_LIST=${_TEST_LIST}" -D |
| 143 | + "TEST_JUNIT_OUTPUT_DIR=${_JUNIT_OUTPUT_DIR}" -D "CTEST_FILE=${ctest_tests_file}" -P |
| 144 | + "${_DOCTEST_DISCOVER_TESTS_SCRIPT}" |
| 145 | + VERBATIM |
| 146 | + ) |
| 147 | + |
| 148 | + file( |
| 149 | + WRITE "${ctest_include_file}" |
| 150 | + "if(EXISTS \"${ctest_tests_file}\")\n" " include(\"${ctest_tests_file}\")\n" "else()\n" |
| 151 | + " add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n" "endif()\n" |
| 152 | + ) |
| 153 | + |
| 154 | + if(NOT CMAKE_VERSION VERSION_LESS 3.10) |
| 155 | + # Add discovered tests to directory TEST_INCLUDE_FILES |
| 156 | + set_property( |
| 157 | + DIRECTORY |
| 158 | + APPEND |
| 159 | + PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}" |
| 160 | + ) |
| 161 | + else() |
| 162 | + # Add discovered tests as directory TEST_INCLUDE_FILE if possible |
| 163 | + get_property( |
| 164 | + test_include_file_set |
| 165 | + DIRECTORY |
| 166 | + PROPERTY TEST_INCLUDE_FILE |
| 167 | + SET |
| 168 | + ) |
| 169 | + if(NOT ${test_include_file_set}) |
| 170 | + set_property(DIRECTORY PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}") |
| 171 | + else() |
| 172 | + message(FATAL_ERROR "Cannot set more than one TEST_INCLUDE_FILE") |
| 173 | + endif() |
| 174 | + endif() |
| 175 | + |
| 176 | +endfunction() |
| 177 | + |
| 178 | +# ################################################################################################## |
| 179 | + |
| 180 | +set(_DOCTEST_DISCOVER_TESTS_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/doctestAddTests.cmake) |
0 commit comments