Catch.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. )
  30. ``catch_discover_tests`` sets up a post-build command on the test executable
  31. that generates the list of tests by parsing the output from running the test
  32. with the ``--list-test-names-only`` argument. This ensures that the full
  33. list of tests is obtained. Since test discovery occurs at build time, it is
  34. not necessary to re-run CMake when the list of tests changes.
  35. However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set
  36. in order to function in a cross-compiling environment.
  37. Additionally, setting properties on tests is somewhat less convenient, since
  38. the tests are not available at CMake time. Additional test properties may be
  39. assigned to the set of tests as a whole using the ``PROPERTIES`` option. If
  40. more fine-grained test control is needed, custom content may be provided
  41. through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES`
  42. directory property. The set of discovered tests is made accessible to such a
  43. script via the ``<target>_TESTS`` variable.
  44. The options are:
  45. ``target``
  46. Specifies the Catch executable, which must be a known CMake executable
  47. target. CMake will substitute the location of the built executable when
  48. running the test.
  49. ``TEST_SPEC arg1...``
  50. Specifies test cases, wildcarded test cases, tags and tag expressions to
  51. pass to the Catch executable with the ``--list-test-names-only`` argument.
  52. ``EXTRA_ARGS arg1...``
  53. Any extra arguments to pass on the command line to each test case.
  54. ``WORKING_DIRECTORY dir``
  55. Specifies the directory in which to run the discovered test cases. If this
  56. option is not provided, the current binary directory is used.
  57. ``TEST_PREFIX prefix``
  58. Specifies a ``prefix`` to be prepended to the name of each discovered test
  59. case. This can be useful when the same test executable is being used in
  60. multiple calls to ``catch_discover_tests()`` but with different
  61. ``TEST_SPEC`` or ``EXTRA_ARGS``.
  62. ``TEST_SUFFIX suffix``
  63. Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
  64. every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
  65. be specified.
  66. ``PROPERTIES name1 value1...``
  67. Specifies additional properties to be set on all tests discovered by this
  68. invocation of ``catch_discover_tests``.
  69. ``TEST_LIST var``
  70. Make the list of tests available in the variable ``var``, rather than the
  71. default ``<target>_TESTS``. This can be useful when the same test
  72. executable is being used in multiple calls to ``catch_discover_tests()``.
  73. Note that this variable is only available in CTest.
  74. #]=======================================================================]
  75. #------------------------------------------------------------------------------
  76. function(catch_discover_tests TARGET)
  77. cmake_parse_arguments(
  78. ""
  79. ""
  80. "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST"
  81. "TEST_SPEC;EXTRA_ARGS;PROPERTIES"
  82. ${ARGN}
  83. )
  84. if(NOT _WORKING_DIRECTORY)
  85. set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  86. endif()
  87. if(NOT _TEST_LIST)
  88. set(_TEST_LIST ${TARGET}_TESTS)
  89. endif()
  90. ## Generate a unique name based on the extra arguments
  91. string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS}")
  92. string(SUBSTRING ${args_hash} 0 7 args_hash)
  93. # Define rule to generate test list for aforementioned test executable
  94. set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake")
  95. set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake")
  96. get_property(crosscompiling_emulator
  97. TARGET ${TARGET}
  98. PROPERTY CROSSCOMPILING_EMULATOR
  99. )
  100. add_custom_command(
  101. TARGET ${TARGET} POST_BUILD
  102. BYPRODUCTS "${ctest_tests_file}"
  103. COMMAND "${CMAKE_COMMAND}"
  104. -D "TEST_TARGET=${TARGET}"
  105. -D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
  106. -D "TEST_EXECUTOR=${crosscompiling_emulator}"
  107. -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}"
  108. -D "TEST_SPEC=${_TEST_SPEC}"
  109. -D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
  110. -D "TEST_PROPERTIES=${_PROPERTIES}"
  111. -D "TEST_PREFIX='${_TEST_PREFIX}'"
  112. -D "TEST_SUFFIX='${_TEST_SUFFIX}'"
  113. -D "TEST_LIST=${_TEST_LIST}"
  114. -D "CTEST_FILE=${ctest_tests_file}"
  115. -P "${_CATCH_DISCOVER_TESTS_SCRIPT}"
  116. VERBATIM
  117. )
  118. file(WRITE "${ctest_include_file}"
  119. "if(EXISTS \"${ctest_tests_file}\")\n"
  120. " include(\"${ctest_tests_file}\")\n"
  121. "else()\n"
  122. " add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n"
  123. "endif()\n"
  124. )
  125. if(NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0")
  126. # Add discovered tests to directory TEST_INCLUDE_FILES
  127. set_property(DIRECTORY
  128. APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
  129. )
  130. else()
  131. # Add discovered tests as directory TEST_INCLUDE_FILE if possible
  132. get_property(test_include_file_set DIRECTORY PROPERTY TEST_INCLUDE_FILE SET)
  133. if (NOT ${test_include_file_set})
  134. set_property(DIRECTORY
  135. PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}"
  136. )
  137. else()
  138. message(FATAL_ERROR
  139. "Cannot set more than one TEST_INCLUDE_FILE"
  140. )
  141. endif()
  142. endif()
  143. endfunction()
  144. ###############################################################################
  145. set(_CATCH_DISCOVER_TESTS_SCRIPT
  146. ${CMAKE_CURRENT_LIST_DIR}/CatchAddTests.cmake
  147. )