| 1 |
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
# file Copyright.txt or https://cmake.org/licensing for details. |
| 3 |
|
| 4 |
#[=======================================================================[.rst: |
| 5 |
Catch |
| 6 |
----- |
| 7 |
|
| 8 |
This module defines a function to help use the Catch test framework. |
| 9 |
|
| 10 |
The :command:`catch_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 Catch 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 Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``. |
| 22 |
|
| 23 |
.. command:: catch_discover_tests |
| 24 |
|
| 25 |
Automatically add tests with CTest by querying the compiled test executable |
| 26 |
for available tests:: |
| 27 |
|
| 28 |
catch_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 |
[TEST_LIST var] |
| 36 |
[REPORTER reporter] |
| 37 |
[OUTPUT_DIR dir] |
| 38 |
[OUTPUT_PREFIX prefix] |
| 39 |
[OUTPUT_SUFFIX suffix] |
| 40 |
[DISCOVERY_MODE <POST_BUILD|PRE_TEST>] |
| 41 |
) |
| 42 |
|
| 43 |
``catch_discover_tests`` sets up a post-build command on the test executable |
| 44 |
that generates the list of tests by parsing the output from running the test |
| 45 |
with the ``--list-test-names-only`` argument. This ensures that the full |
| 46 |
list of tests is obtained. Since test discovery occurs at build time, it is |
| 47 |
not necessary to re-run CMake when the list of tests changes. |
| 48 |
However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set |
| 49 |
in order to function in a cross-compiling environment. |
| 50 |
|
| 51 |
Additionally, setting properties on tests is somewhat less convenient, since |
| 52 |
the tests are not available at CMake time. Additional test properties may be |
| 53 |
assigned to the set of tests as a whole using the ``PROPERTIES`` option. If |
| 54 |
more fine-grained test control is needed, custom content may be provided |
| 55 |
through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES` |
| 56 |
directory property. The set of discovered tests is made accessible to such a |
| 57 |
script via the ``<target>_TESTS`` variable. |
| 58 |
|
| 59 |
The options are: |
| 60 |
|
| 61 |
``target`` |
| 62 |
Specifies the Catch executable, which must be a known CMake executable |
| 63 |
target. CMake will substitute the location of the built executable when |
| 64 |
running the test. |
| 65 |
|
| 66 |
``TEST_SPEC arg1...`` |
| 67 |
Specifies test cases, wildcarded test cases, tags and tag expressions to |
| 68 |
pass to the Catch executable with the ``--list-test-names-only`` argument. |
| 69 |
|
| 70 |
``EXTRA_ARGS arg1...`` |
| 71 |
Any extra arguments to pass on the command line to each test case. |
| 72 |
|
| 73 |
``WORKING_DIRECTORY dir`` |
| 74 |
Specifies the directory in which to run the discovered test cases. If this |
| 75 |
option is not provided, the current binary directory is used. |
| 76 |
|
| 77 |
``TEST_PREFIX prefix`` |
| 78 |
Specifies a ``prefix`` to be prepended to the name of each discovered test |
| 79 |
case. This can be useful when the same test executable is being used in |
| 80 |
multiple calls to ``catch_discover_tests()`` but with different |
| 81 |
``TEST_SPEC`` or ``EXTRA_ARGS``. |
| 82 |
|
| 83 |
``TEST_SUFFIX suffix`` |
| 84 |
Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of |
| 85 |
every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may |
| 86 |
be specified. |
| 87 |
|
| 88 |
``PROPERTIES name1 value1...`` |
| 89 |
Specifies additional properties to be set on all tests discovered by this |
| 90 |
invocation of ``catch_discover_tests``. |
| 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 ``catch_discover_tests()``. |
| 96 |
Note that this variable is only available in CTest. |
| 97 |
|
| 98 |
``REPORTER reporter`` |
| 99 |
Use the specified reporter when running the test case. The reporter will |
| 100 |
be passed to the Catch executable as ``--reporter reporter``. |
| 101 |
|
| 102 |
``OUTPUT_DIR dir`` |
| 103 |
If specified, the parameter is passed along as |
| 104 |
``--out dir/<test_name>`` to Catch executable. The actual file name is the |
| 105 |
same as the test name. This should be used instead of |
| 106 |
``EXTRA_ARGS --out foo`` to avoid race conditions writing the result output |
| 107 |
when using parallel test execution. |
| 108 |
|
| 109 |
``OUTPUT_PREFIX prefix`` |
| 110 |
May be used in conjunction with ``OUTPUT_DIR``. |
| 111 |
If specified, ``prefix`` is added to each output file name, like so |
| 112 |
``--out dir/prefix<test_name>``. |
| 113 |
|
| 114 |
``OUTPUT_SUFFIX suffix`` |
| 115 |
May be used in conjunction with ``OUTPUT_DIR``. |
| 116 |
If specified, ``suffix`` is added to each output file name, like so |
| 117 |
``--out dir/<test_name>suffix``. This can be used to add a file extension to |
| 118 |
the output e.g. ".xml". |
| 119 |
|
| 120 |
``DL_PATHS path...`` |
| 121 |
Specifies paths that need to be set for the dynamic linker to find shared |
| 122 |
libraries/DLLs when running the test executable (PATH/LD_LIBRARY_PATH respectively). |
| 123 |
These paths will both be set when retrieving the list of test cases from the |
| 124 |
test executable and when the tests are executed themselves. This requires |
| 125 |
cmake/ctest >= 3.22. |
| 126 |
|
| 127 |
`DISCOVERY_MODE mode`` |
| 128 |
Provides control over when ``catch_discover_tests`` performs test discovery. |
| 129 |
By default, ``POST_BUILD`` sets up a post-build command to perform test discovery |
| 130 |
at build time. In certain scenarios, like cross-compiling, this ``POST_BUILD`` |
| 131 |
behavior is not desirable. By contrast, ``PRE_TEST`` delays test discovery until |
| 132 |
just prior to test execution. This way test discovery occurs in the target environment |
| 133 |
where the test has a better chance at finding appropriate runtime dependencies. |
| 134 |
|
| 135 |
``DISCOVERY_MODE`` defaults to the value of the |
| 136 |
``CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE`` variable if it is not passed when |
| 137 |
calling ``catch_discover_tests``. This provides a mechanism for globally selecting |
| 138 |
a preferred test discovery behavior without having to modify each call site. |
| 139 |
|
| 140 |
#]=======================================================================] |
| 141 |
|
| 142 |
#------------------------------------------------------------------------------ |
| 143 |
function(catch_discover_tests TARGET) |
| 144 |
|
| 145 |
cmake_parse_arguments( |
| 146 |
"" |
| 147 |
"" |
| 148 |
"TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;REPORTER;OUTPUT_DIR;OUTPUT_PREFIX;OUTPUT_SUFFIX;DISCOVERY_MODE" |
| 149 |
"TEST_SPEC;EXTRA_ARGS;PROPERTIES;DL_PATHS" |
| 150 |
${ARGN} |
| 151 |
) |
| 152 |
|
| 153 |
if(NOT _WORKING_DIRECTORY) |
| 154 |
set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") |
| 155 |
endif() |
| 156 |
if(NOT _TEST_LIST) |
| 157 |
set(_TEST_LIST ${TARGET}_TESTS) |
| 158 |
endif() |
| 159 |
if (_DL_PATHS) |
| 160 |
if(${CMAKE_VERSION} VERSION_LESS "3.22.0") |
| 161 |
message(FATAL_ERROR "The DL_PATHS option requires at least cmake 3.22") |
| 162 |
endif() |
| 163 |
endif() |
| 164 |
if(NOT _DISCOVERY_MODE) |
| 165 |
if(NOT CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE) |
| 166 |
set(CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE "POST_BUILD") |
| 167 |
endif() |
| 168 |
set(_DISCOVERY_MODE ${CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE}) |
| 169 |
endif() |
| 170 |
if (NOT _DISCOVERY_MODE MATCHES "^(POST_BUILD|PRE_TEST)$") |
| 171 |
message(FATAL_ERROR "Unknown DISCOVERY_MODE: ${_DISCOVERY_MODE}") |
| 172 |
endif() |
| 173 |
|
| 174 |
## Generate a unique name based on the extra arguments |
| 175 |
string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS} ${_REPORTER} ${_OUTPUT_DIR} ${_OUTPUT_PREFIX} ${_OUTPUT_SUFFIX}") |
| 176 |
string(SUBSTRING ${args_hash} 0 7 args_hash) |
| 177 |
|
| 178 |
# Define rule to generate test list for aforementioned test executable |
| 179 |
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake") |
| 180 |
set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake") |
| 181 |
get_property(crosscompiling_emulator |
| 182 |
TARGET ${TARGET} |
| 183 |
PROPERTY CROSSCOMPILING_EMULATOR |
| 184 |
) |
| 185 |
|
| 186 |
if(_DISCOVERY_MODE STREQUAL "POST_BUILD") |
| 187 |
add_custom_command( |
| 188 |
TARGET ${TARGET} POST_BUILD |
| 189 |
BYPRODUCTS "${ctest_tests_file}" |
| 190 |
COMMAND "${CMAKE_COMMAND}" |
| 191 |
-D "TEST_TARGET=${TARGET}" |
| 192 |
-D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>" |
| 193 |
-D "TEST_EXECUTOR=${crosscompiling_emulator}" |
| 194 |
-D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}" |
| 195 |
-D "TEST_SPEC=${_TEST_SPEC}" |
| 196 |
-D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}" |
| 197 |
-D "TEST_PROPERTIES=${_PROPERTIES}" |
| 198 |
-D "TEST_PREFIX=${_TEST_PREFIX}" |
| 199 |
-D "TEST_SUFFIX=${_TEST_SUFFIX}" |
| 200 |
-D "TEST_LIST=${_TEST_LIST}" |
| 201 |
-D "TEST_REPORTER=${_REPORTER}" |
| 202 |
-D "TEST_OUTPUT_DIR=${_OUTPUT_DIR}" |
| 203 |
-D "TEST_OUTPUT_PREFIX=${_OUTPUT_PREFIX}" |
| 204 |
-D "TEST_OUTPUT_SUFFIX=${_OUTPUT_SUFFIX}" |
| 205 |
-D "TEST_DL_PATHS=${_DL_PATHS}" |
| 206 |
-D "CTEST_FILE=${ctest_tests_file}" |
| 207 |
-P "${_CATCH_DISCOVER_TESTS_SCRIPT}" |
| 208 |
VERBATIM |
| 209 |
) |
| 210 |
|
| 211 |
file(WRITE "${ctest_include_file}" |
| 212 |
"if(EXISTS \"${ctest_tests_file}\")\n" |
| 213 |
" include(\"${ctest_tests_file}\")\n" |
| 214 |
"else()\n" |
| 215 |
" add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n" |
| 216 |
"endif()\n" |
| 217 |
) |
| 218 |
|
| 219 |
elseif(_DISCOVERY_MODE STREQUAL "PRE_TEST") |
| 220 |
|
| 221 |
string(CONCAT ctest_include_content |
| 222 |
"if(EXISTS \"$<TARGET_FILE:${TARGET}>\")" "\n" |
| 223 |
" if(NOT EXISTS \"${ctest_tests_file}\" OR" "\n" |
| 224 |
" NOT \"${ctest_tests_file}\" IS_NEWER_THAN \"$<TARGET_FILE:${TARGET}>\" OR\n" |
| 225 |
" NOT \"${ctest_tests_file}\" IS_NEWER_THAN \"\${CMAKE_CURRENT_LIST_FILE}\")\n" |
| 226 |
" include(\"${_CATCH_DISCOVER_TESTS_SCRIPT}\")" "\n" |
| 227 |
" catch_discover_tests_impl(" "\n" |
| 228 |
" TEST_EXECUTABLE" " [==[" "$<TARGET_FILE:${TARGET}>" "]==]" "\n" |
| 229 |
" TEST_EXECUTOR" " [==[" "${crosscompiling_emulator}" "]==]" "\n" |
| 230 |
" TEST_WORKING_DIR" " [==[" "${_WORKING_DIRECTORY}" "]==]" "\n" |
| 231 |
" TEST_SPEC" " [==[" "${_TEST_SPEC}" "]==]" "\n" |
| 232 |
" TEST_EXTRA_ARGS" " [==[" "${_EXTRA_ARGS}" "]==]" "\n" |
| 233 |
" TEST_PROPERTIES" " [==[" "${_PROPERTIES}" "]==]" "\n" |
| 234 |
" TEST_PREFIX" " [==[" "${_TEST_PREFIX}" "]==]" "\n" |
| 235 |
" TEST_SUFFIX" " [==[" "${_TEST_SUFFIX}" "]==]" "\n" |
| 236 |
" TEST_LIST" " [==[" "${_TEST_LIST}" "]==]" "\n" |
| 237 |
" TEST_REPORTER" " [==[" "${_REPORTER}" "]==]" "\n" |
| 238 |
" TEST_OUTPUT_DIR" " [==[" "${_OUTPUT_DIR}" "]==]" "\n" |
| 239 |
" TEST_OUTPUT_PREFIX" " [==[" "${_OUTPUT_PREFIX}" "]==]" "\n" |
| 240 |
" TEST_OUTPUT_SUFFIX" " [==[" "${_OUTPUT_SUFFIX}" "]==]" "\n" |
| 241 |
" CTEST_FILE" " [==[" "${ctest_tests_file}" "]==]" "\n" |
| 242 |
" TEST_DL_PATHS" " [==[" "${_DL_PATHS}" "]==]" "\n" |
| 243 |
" CTEST_FILE" " [==[" "${CTEST_FILE}" "]==]" "\n" |
| 244 |
" )" "\n" |
| 245 |
" endif()" "\n" |
| 246 |
" include(\"${ctest_tests_file}\")" "\n" |
| 247 |
"else()" "\n" |
| 248 |
" add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)" "\n" |
| 249 |
"endif()" "\n" |
| 250 |
) |
| 251 |
|
| 252 |
file(GENERATE OUTPUT "${ctest_include_file}" CONTENT "${ctest_include_content}") |
| 253 |
endif() |
| 254 |
|
| 255 |
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0") |
| 256 |
# Add discovered tests to directory TEST_INCLUDE_FILES |
| 257 |
set_property(DIRECTORY |
| 258 |
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}" |
| 259 |
) |
| 260 |
else() |
| 261 |
# Add discovered tests as directory TEST_INCLUDE_FILE if possible |
| 262 |
get_property(test_include_file_set DIRECTORY PROPERTY TEST_INCLUDE_FILE SET) |
| 263 |
if (NOT ${test_include_file_set}) |
| 264 |
set_property(DIRECTORY |
| 265 |
PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}" |
| 266 |
) |
| 267 |
else() |
| 268 |
message(FATAL_ERROR "Cannot set more than one TEST_INCLUDE_FILE") |
| 269 |
endif() |
| 270 |
endif() |
| 271 |
|
| 272 |
endfunction() |
| 273 |
|
| 274 |
############################################################################### |
| 275 |
|
| 276 |
set(_CATCH_DISCOVER_TESTS_SCRIPT |
| 277 |
${CMAKE_CURRENT_LIST_DIR}/CatchAddTests.cmake |
| 278 |
CACHE INTERNAL "Catch2 full path to CatchAddTests.cmake helper file" |
| 279 |
) |