GoogleTest.cmake 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. GoogleTest
  5. ----------
  6. This module defines functions to help use the Google Test infrastructure. Two
  7. mechanisms for adding tests are provided. :command:`gtest_add_tests` has been
  8. around for some time, originally via ``find_package(GTest)``.
  9. :command:`gtest_discover_tests` was introduced in CMake 3.10.
  10. The (older) :command:`gtest_add_tests` scans source files to identify tests.
  11. This is usually effective, with some caveats, including in cross-compiling
  12. environments, and makes setting additional properties on tests more convenient.
  13. However, its handling of parameterized tests is less comprehensive, and it
  14. requires re-running CMake to detect changes to the list of tests.
  15. The (newer) :command:`gtest_discover_tests` discovers tests by asking the
  16. compiled test executable to enumerate its tests. This is more robust and
  17. provides better handling of parameterized tests, and does not require CMake
  18. to be re-run when tests change. However, it may not work in a cross-compiling
  19. environment, and setting test properties is less convenient.
  20. More details can be found in the documentation of the respective functions.
  21. Both commands are intended to replace use of :command:`add_test` to register
  22. tests, and will create a separate CTest test for each Google Test test case.
  23. Note that this is in some cases less efficient, as common set-up and tear-down
  24. logic cannot be shared by multiple test cases executing in the same instance.
  25. However, it provides more fine-grained pass/fail information to CTest, which is
  26. usually considered as more beneficial. By default, the CTest test name is the
  27. same as the Google Test name (i.e. ``suite.testcase``); see also
  28. ``TEST_PREFIX`` and ``TEST_SUFFIX``.
  29. .. command:: gtest_add_tests
  30. Automatically add tests with CTest by scanning source code for Google Test
  31. macros::
  32. gtest_add_tests(TARGET target
  33. [SOURCES src1...]
  34. [EXTRA_ARGS arg1...]
  35. [WORKING_DIRECTORY dir]
  36. [TEST_PREFIX prefix]
  37. [TEST_SUFFIX suffix]
  38. [SKIP_DEPENDENCY]
  39. [TEST_LIST outVar]
  40. )
  41. ``gtest_add_tests`` attempts to identify tests by scanning source files.
  42. Although this is generally effective, it uses only a basic regular expression
  43. match, which can be defeated by atypical test declarations, and is unable to
  44. fully "split" parameterized tests. Additionally, it requires that CMake be
  45. re-run to discover any newly added, removed or renamed tests (by default,
  46. this means that CMake is re-run when any test source file is changed, but see
  47. ``SKIP_DEPENDENCY``). However, it has the advantage of declaring tests at
  48. CMake time, which somewhat simplifies setting additional properties on tests,
  49. and always works in a cross-compiling environment.
  50. The options are:
  51. ``TARGET target``
  52. Specifies the Google Test executable, which must be a known CMake
  53. executable target. CMake will substitute the location of the built
  54. executable when running the test.
  55. ``SOURCES src1...``
  56. When provided, only the listed files will be scanned for test cases. If
  57. this option is not given, the :prop_tgt:`SOURCES` property of the
  58. specified ``target`` will be used to obtain the list of sources.
  59. ``EXTRA_ARGS arg1...``
  60. Any extra arguments to pass on the command line to each test case.
  61. ``WORKING_DIRECTORY dir``
  62. Specifies the directory in which to run the discovered test cases. If this
  63. option is not provided, the current binary directory is used.
  64. ``TEST_PREFIX prefix``
  65. Specifies a ``prefix`` to be prepended to the name of each discovered test
  66. case. This can be useful when the same source files are being used in
  67. multiple calls to ``gtest_add_test()`` but with different ``EXTRA_ARGS``.
  68. ``TEST_SUFFIX suffix``
  69. Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
  70. every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
  71. be specified.
  72. ``SKIP_DEPENDENCY``
  73. Normally, the function creates a dependency which will cause CMake to be
  74. re-run if any of the sources being scanned are changed. This is to ensure
  75. that the list of discovered tests is updated. If this behavior is not
  76. desired (as may be the case while actually writing the test cases), this
  77. option can be used to prevent the dependency from being added.
  78. ``TEST_LIST outVar``
  79. The variable named by ``outVar`` will be populated in the calling scope
  80. with the list of discovered test cases. This allows the caller to do
  81. things like manipulate test properties of the discovered tests.
  82. .. code-block:: cmake
  83. include(GoogleTest)
  84. add_executable(FooTest FooUnitTest.cxx)
  85. gtest_add_tests(TARGET FooTest
  86. TEST_SUFFIX .noArgs
  87. TEST_LIST noArgsTests
  88. )
  89. gtest_add_tests(TARGET FooTest
  90. EXTRA_ARGS --someArg someValue
  91. TEST_SUFFIX .withArgs
  92. TEST_LIST withArgsTests
  93. )
  94. set_tests_properties(${noArgsTests} PROPERTIES TIMEOUT 10)
  95. set_tests_properties(${withArgsTests} PROPERTIES TIMEOUT 20)
  96. For backward compatibility, the following form is also supported::
  97. gtest_add_tests(exe args files...)
  98. ``exe``
  99. The path to the test executable or the name of a CMake target.
  100. ``args``
  101. A ;-list of extra arguments to be passed to executable. The entire
  102. list must be passed as a single argument. Enclose it in quotes,
  103. or pass ``""`` for no arguments.
  104. ``files...``
  105. A list of source files to search for tests and test fixtures.
  106. Alternatively, use ``AUTO`` to specify that ``exe`` is the name
  107. of a CMake executable target whose sources should be scanned.
  108. .. code-block:: cmake
  109. include(GoogleTest)
  110. set(FooTestArgs --foo 1 --bar 2)
  111. add_executable(FooTest FooUnitTest.cxx)
  112. gtest_add_tests(FooTest "${FooTestArgs}" AUTO)
  113. .. command:: gtest_discover_tests
  114. Automatically add tests with CTest by querying the compiled test executable
  115. for available tests::
  116. gtest_discover_tests(target
  117. [EXTRA_ARGS arg1...]
  118. [WORKING_DIRECTORY dir]
  119. [TEST_PREFIX prefix]
  120. [TEST_SUFFIX suffix]
  121. [NO_PRETTY_TYPES] [NO_PRETTY_VALUES]
  122. [PROPERTIES name1 value1...]
  123. [TEST_LIST var]
  124. [DISCOVERY_TIMEOUT seconds]
  125. )
  126. ``gtest_discover_tests`` sets up a post-build command on the test executable
  127. that generates the list of tests by parsing the output from running the test
  128. with the ``--gtest_list_tests`` argument. Compared to the source parsing
  129. approach of :command:`gtest_add_tests`, this ensures that the full list of
  130. tests, including instantiations of parameterized tests, is obtained. Since
  131. test discovery occurs at build time, it is not necessary to re-run CMake when
  132. the list of tests changes.
  133. However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set
  134. in order to function in a cross-compiling environment.
  135. Additionally, setting properties on tests is somewhat less convenient, since
  136. the tests are not available at CMake time. Additional test properties may be
  137. assigned to the set of tests as a whole using the ``PROPERTIES`` option. If
  138. more fine-grained test control is needed, custom content may be provided
  139. through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES`
  140. directory property. The set of discovered tests is made accessible to such a
  141. script via the ``<target>_TESTS`` variable.
  142. The options are:
  143. ``target``
  144. Specifies the Google Test executable, which must be a known CMake
  145. executable target. CMake will substitute the location of the built
  146. executable when running the test.
  147. ``EXTRA_ARGS arg1...``
  148. Any extra arguments to pass on the command line to each test case.
  149. ``WORKING_DIRECTORY dir``
  150. Specifies the directory in which to run the discovered test cases. If this
  151. option is not provided, the current binary directory is used.
  152. ``TEST_PREFIX prefix``
  153. Specifies a ``prefix`` to be prepended to the name of each discovered test
  154. case. This can be useful when the same test executable is being used in
  155. multiple calls to ``gtest_discover_tests()`` but with different
  156. ``EXTRA_ARGS``.
  157. ``TEST_SUFFIX suffix``
  158. Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
  159. every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
  160. be specified.
  161. ``NO_PRETTY_TYPES``
  162. By default, the type index of type-parameterized tests is replaced by the
  163. actual type name in the CTest test name. If this behavior is undesirable
  164. (e.g. because the type names are unwieldy), this option will suppress this
  165. behavior.
  166. ``NO_PRETTY_VALUES``
  167. By default, the value index of value-parameterized tests is replaced by the
  168. actual value in the CTest test name. If this behavior is undesirable
  169. (e.g. because the value strings are unwieldy), this option will suppress
  170. this behavior.
  171. ``PROPERTIES name1 value1...``
  172. Specifies additional properties to be set on all tests discovered by this
  173. invocation of ``gtest_discover_tests``.
  174. ``TEST_LIST var``
  175. Make the list of tests available in the variable ``var``, rather than the
  176. default ``<target>_TESTS``. This can be useful when the same test
  177. executable is being used in multiple calls to ``gtest_discover_tests()``.
  178. Note that this variable is only available in CTest.
  179. ``DISCOVERY_TIMEOUT num``
  180. Specifies how long (in seconds) CMake will wait for the test to enumerate
  181. available tests. If the test takes longer than this, discovery (and your
  182. build) will fail. Most test executables will enumerate their tests very
  183. quickly, but under some exceptional circumstances, a test may require a
  184. longer timeout. The default is 5. See also the ``TIMEOUT`` option of
  185. :command:`execute_process`.
  186. .. note::
  187. In CMake versions 3.10.1 and 3.10.2, this option was called ``TIMEOUT``.
  188. This clashed with the ``TIMEOUT`` test property, which is one of the
  189. common properties that would be set with the ``PROPERTIES`` keyword,
  190. usually leading to legal but unintended behavior. The keyword was
  191. changed to ``DISCOVERY_TIMEOUT`` in CMake 3.10.3 to address this
  192. problem. The ambiguous behavior of the ``TIMEOUT`` keyword in 3.10.1
  193. and 3.10.2 has not been preserved.
  194. #]=======================================================================]
  195. # Save project's policies
  196. cmake_policy(PUSH)
  197. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  198. #------------------------------------------------------------------------------
  199. function(gtest_add_tests)
  200. if (ARGC LESS 1)
  201. message(FATAL_ERROR "No arguments supplied to gtest_add_tests()")
  202. endif()
  203. set(options
  204. SKIP_DEPENDENCY
  205. )
  206. set(oneValueArgs
  207. TARGET
  208. WORKING_DIRECTORY
  209. TEST_PREFIX
  210. TEST_SUFFIX
  211. TEST_LIST
  212. )
  213. set(multiValueArgs
  214. SOURCES
  215. EXTRA_ARGS
  216. )
  217. set(allKeywords ${options} ${oneValueArgs} ${multiValueArgs})
  218. unset(sources)
  219. if("${ARGV0}" IN_LIST allKeywords)
  220. cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  221. set(autoAddSources YES)
  222. else()
  223. # Non-keyword syntax, convert to keyword form
  224. if (ARGC LESS 3)
  225. message(FATAL_ERROR "gtest_add_tests() without keyword options requires at least 3 arguments")
  226. endif()
  227. set(ARGS_TARGET "${ARGV0}")
  228. set(ARGS_EXTRA_ARGS "${ARGV1}")
  229. if(NOT "${ARGV2}" STREQUAL "AUTO")
  230. set(ARGS_SOURCES "${ARGV}")
  231. list(REMOVE_AT ARGS_SOURCES 0 1)
  232. endif()
  233. endif()
  234. # The non-keyword syntax allows the first argument to be an arbitrary
  235. # executable rather than a target if source files are also provided. In all
  236. # other cases, both forms require a target.
  237. if(NOT TARGET "${ARGS_TARGET}" AND NOT ARGS_SOURCES)
  238. message(FATAL_ERROR "${ARGS_TARGET} does not define an existing CMake target")
  239. endif()
  240. if(NOT ARGS_WORKING_DIRECTORY)
  241. unset(workDir)
  242. else()
  243. set(workDir WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}")
  244. endif()
  245. if(NOT ARGS_SOURCES)
  246. get_property(ARGS_SOURCES TARGET ${ARGS_TARGET} PROPERTY SOURCES)
  247. endif()
  248. unset(testList)
  249. set(gtest_case_name_regex ".*\\( *([A-Za-z_0-9]+) *, *([A-Za-z_0-9]+) *\\).*")
  250. set(gtest_test_type_regex "(TYPED_TEST|TEST_?[FP]?)")
  251. foreach(source IN LISTS ARGS_SOURCES)
  252. if(NOT ARGS_SKIP_DEPENDENCY)
  253. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${source})
  254. endif()
  255. file(READ "${source}" contents)
  256. string(REGEX MATCHALL "${gtest_test_type_regex} *\\(([A-Za-z_0-9 ,]+)\\)" found_tests "${contents}")
  257. foreach(hit ${found_tests})
  258. string(REGEX MATCH "${gtest_test_type_regex}" test_type ${hit})
  259. # Parameterized tests have a different signature for the filter
  260. if("x${test_type}" STREQUAL "xTEST_P")
  261. string(REGEX REPLACE ${gtest_case_name_regex} "*/\\1.\\2/*" gtest_test_name ${hit})
  262. elseif("x${test_type}" STREQUAL "xTEST_F" OR "x${test_type}" STREQUAL "xTEST")
  263. string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" gtest_test_name ${hit})
  264. elseif("x${test_type}" STREQUAL "xTYPED_TEST")
  265. string(REGEX REPLACE ${gtest_case_name_regex} "\\1/*.\\2" gtest_test_name ${hit})
  266. else()
  267. message(WARNING "Could not parse GTest ${hit} for adding to CTest.")
  268. continue()
  269. endif()
  270. # Make sure tests disabled in GTest get disabled in CTest
  271. if(gtest_test_name MATCHES "(^|\\.)DISABLED_")
  272. # Add the disabled test if CMake is new enough
  273. # Note that this check is to allow backwards compatibility so this
  274. # module can be copied locally in projects to use with older CMake
  275. # versions
  276. if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.8.20170401)
  277. string(REGEX REPLACE
  278. "(^|\\.)DISABLED_" "\\1"
  279. orig_test_name "${gtest_test_name}"
  280. )
  281. set(ctest_test_name
  282. ${ARGS_TEST_PREFIX}${orig_test_name}${ARGS_TEST_SUFFIX}
  283. )
  284. add_test(NAME ${ctest_test_name}
  285. ${workDir}
  286. COMMAND ${ARGS_TARGET}
  287. --gtest_also_run_disabled_tests
  288. --gtest_filter=${gtest_test_name}
  289. ${ARGS_EXTRA_ARGS}
  290. )
  291. set_tests_properties(${ctest_test_name} PROPERTIES DISABLED TRUE)
  292. list(APPEND testList ${ctest_test_name})
  293. endif()
  294. else()
  295. set(ctest_test_name ${ARGS_TEST_PREFIX}${gtest_test_name}${ARGS_TEST_SUFFIX})
  296. add_test(NAME ${ctest_test_name}
  297. ${workDir}
  298. COMMAND ${ARGS_TARGET}
  299. --gtest_filter=${gtest_test_name}
  300. ${ARGS_EXTRA_ARGS}
  301. )
  302. list(APPEND testList ${ctest_test_name})
  303. endif()
  304. endforeach()
  305. endforeach()
  306. if(ARGS_TEST_LIST)
  307. set(${ARGS_TEST_LIST} ${testList} PARENT_SCOPE)
  308. endif()
  309. endfunction()
  310. #------------------------------------------------------------------------------
  311. function(gtest_discover_tests TARGET)
  312. cmake_parse_arguments(
  313. ""
  314. "NO_PRETTY_TYPES;NO_PRETTY_VALUES"
  315. "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;DISCOVERY_TIMEOUT"
  316. "EXTRA_ARGS;PROPERTIES"
  317. ${ARGN}
  318. )
  319. if(NOT _WORKING_DIRECTORY)
  320. set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
  321. endif()
  322. if(NOT _TEST_LIST)
  323. set(_TEST_LIST ${TARGET}_TESTS)
  324. endif()
  325. if(NOT _DISCOVERY_TIMEOUT)
  326. set(_DISCOVERY_TIMEOUT 5)
  327. endif()
  328. get_property(
  329. has_counter
  330. TARGET ${TARGET}
  331. PROPERTY CTEST_DISCOVERED_TEST_COUNTER
  332. SET
  333. )
  334. if(has_counter)
  335. get_property(
  336. counter
  337. TARGET ${TARGET}
  338. PROPERTY CTEST_DISCOVERED_TEST_COUNTER
  339. )
  340. math(EXPR counter "${counter} + 1")
  341. else()
  342. set(counter 1)
  343. endif()
  344. set_property(
  345. TARGET ${TARGET}
  346. PROPERTY CTEST_DISCOVERED_TEST_COUNTER
  347. ${counter}
  348. )
  349. # Define rule to generate test list for aforementioned test executable
  350. set(ctest_file_base "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}[${counter}]")
  351. set(ctest_include_file "${ctest_file_base}_include.cmake")
  352. set(ctest_tests_file "${ctest_file_base}_tests.cmake")
  353. get_property(crosscompiling_emulator
  354. TARGET ${TARGET}
  355. PROPERTY CROSSCOMPILING_EMULATOR
  356. )
  357. add_custom_command(
  358. TARGET ${TARGET} POST_BUILD
  359. BYPRODUCTS "${ctest_tests_file}"
  360. COMMAND "${CMAKE_COMMAND}"
  361. -D "TEST_TARGET=${TARGET}"
  362. -D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
  363. -D "TEST_EXECUTOR=${crosscompiling_emulator}"
  364. -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}"
  365. -D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
  366. -D "TEST_PROPERTIES=${_PROPERTIES}"
  367. -D "TEST_PREFIX=${_TEST_PREFIX}"
  368. -D "TEST_SUFFIX=${_TEST_SUFFIX}"
  369. -D "NO_PRETTY_TYPES=${_NO_PRETTY_TYPES}"
  370. -D "NO_PRETTY_VALUES=${_NO_PRETTY_VALUES}"
  371. -D "TEST_LIST=${_TEST_LIST}"
  372. -D "CTEST_FILE=${ctest_tests_file}"
  373. -D "TEST_DISCOVERY_TIMEOUT=${_DISCOVERY_TIMEOUT}"
  374. -P "${_GOOGLETEST_DISCOVER_TESTS_SCRIPT}"
  375. VERBATIM
  376. )
  377. file(WRITE "${ctest_include_file}"
  378. "if(EXISTS \"${ctest_tests_file}\")\n"
  379. " include(\"${ctest_tests_file}\")\n"
  380. "else()\n"
  381. " add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)\n"
  382. "endif()\n"
  383. )
  384. # Add discovered tests to directory TEST_INCLUDE_FILES
  385. set_property(DIRECTORY
  386. APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
  387. )
  388. endfunction()
  389. ###############################################################################
  390. set(_GOOGLETEST_DISCOVER_TESTS_SCRIPT
  391. ${CMAKE_CURRENT_LIST_DIR}/GoogleTestAddTests.cmake
  392. )
  393. # Restore project's policies
  394. cmake_policy(POP)