doctest.cmake 7.6 KB

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