Catch.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #]=======================================================================]
  97. #------------------------------------------------------------------------------
  98. function(catch_discover_tests TARGET)
  99. cmake_parse_arguments(
  100. ""
  101. ""
  102. "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;REPORTER;OUTPUT_DIR;OUTPUT_PREFIX;OUTPUT_SUFFIX"
  103. "TEST_SPEC;EXTRA_ARGS;PROPERTIES"
  104. ${ARGN}
  105. )
  106. if(NOT _WORKING_DIRECTORY)
  107. set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  108. endif()
  109. if(NOT _TEST_LIST)
  110. set(_TEST_LIST ${TARGET}_TESTS)
  111. endif()
  112. ## Generate a unique name based on the extra arguments
  113. string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS} ${_REPORTER} ${_OUTPUT_DIR} ${_OUTPUT_PREFIX} ${_OUTPUT_SUFFIX}")
  114. string(SUBSTRING ${args_hash} 0 7 args_hash)
  115. # Define rule to generate test list for aforementioned test executable
  116. set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake")
  117. set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake")
  118. get_property(crosscompiling_emulator
  119. TARGET ${TARGET}
  120. PROPERTY CROSSCOMPILING_EMULATOR
  121. )
  122. add_custom_command(
  123. TARGET ${TARGET} POST_BUILD
  124. BYPRODUCTS "${ctest_tests_file}"
  125. COMMAND "${CMAKE_COMMAND}"
  126. -D "TEST_TARGET=${TARGET}"
  127. -D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
  128. -D "TEST_EXECUTOR=${crosscompiling_emulator}"
  129. -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}"
  130. -D "TEST_SPEC=${_TEST_SPEC}"
  131. -D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
  132. -D "TEST_PROPERTIES=${_PROPERTIES}"
  133. -D "TEST_PREFIX=${_TEST_PREFIX}"
  134. -D "TEST_SUFFIX=${_TEST_SUFFIX}"
  135. -D "TEST_LIST=${_TEST_LIST}"
  136. -D "TEST_REPORTER=${_REPORTER}"
  137. -D "TEST_OUTPUT_DIR=${_OUTPUT_DIR}"
  138. -D "TEST_OUTPUT_PREFIX=${_OUTPUT_PREFIX}"
  139. -D "TEST_OUTPUT_SUFFIX=${_OUTPUT_SUFFIX}"
  140. -D "CTEST_FILE=${ctest_tests_file}"
  141. -P "${_CATCH_DISCOVER_TESTS_SCRIPT}"
  142. VERBATIM
  143. )
  144. file(WRITE "${ctest_include_file}"
  145. "if(EXISTS \"${ctest_tests_file}\")\n"
  146. " include(\"${ctest_tests_file}\")\n"
  147. "else()\n"
  148. " add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n"
  149. "endif()\n"
  150. )
  151. if(NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0")
  152. # Add discovered tests to directory TEST_INCLUDE_FILES
  153. set_property(DIRECTORY
  154. APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
  155. )
  156. else()
  157. # Add discovered tests as directory TEST_INCLUDE_FILE if possible
  158. get_property(test_include_file_set DIRECTORY PROPERTY TEST_INCLUDE_FILE SET)
  159. if (NOT ${test_include_file_set})
  160. set_property(DIRECTORY
  161. PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}"
  162. )
  163. else()
  164. message(FATAL_ERROR
  165. "Cannot set more than one TEST_INCLUDE_FILE"
  166. )
  167. endif()
  168. endif()
  169. endfunction()
  170. ###############################################################################
  171. set(_CATCH_DISCOVER_TESTS_SCRIPT
  172. ${CMAKE_CURRENT_LIST_DIR}/CatchAddTests.cmake
  173. CACHE INTERNAL "Catch2 full path to CatchAddTests.cmake helper file"
  174. )