Catch.cmake 9.0 KB

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