add_test.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. add_test
  2. --------
  3. Add a test to the project to be run by :manual:`ctest(1)`.
  4. .. code-block:: cmake
  5. add_test(NAME <name> COMMAND <command> [<arg>...]
  6. [CONFIGURATIONS <config>...]
  7. [WORKING_DIRECTORY <dir>]
  8. [COMMAND_EXPAND_LISTS])
  9. Adds a test called ``<name>``. The test name may contain arbitrary
  10. characters, expressed as a :ref:`Quoted Argument` or :ref:`Bracket Argument`
  11. if necessary. See policy :policy:`CMP0110`.
  12. CMake only generates tests if the :command:`enable_testing` command has been
  13. invoked. The :module:`CTest` module invokes ``enable_testing`` automatically
  14. unless ``BUILD_TESTING`` is set to ``OFF``.
  15. Tests added with the ``add_test(NAME)`` signature support using
  16. :manual:`generator expressions <cmake-generator-expressions(7)>`
  17. in test properties set by :command:`set_property(TEST)` or
  18. :command:`set_tests_properties`. Test properties may only be set in the
  19. directory the test is created in.
  20. ``add_test`` options are:
  21. ``COMMAND``
  22. Specify the test command-line.
  23. If ``<command>`` specifies an executable target created by
  24. :command:`add_executable`:
  25. * It will automatically be replaced by the location of the executable
  26. created at build time.
  27. * .. versionadded:: 3.3
  28. The target's :prop_tgt:`CROSSCOMPILING_EMULATOR`, if set, will be
  29. used to run the command on the host::
  30. <emulator> <command>
  31. .. versionchanged:: 3.29
  32. The emulator is used only when
  33. :variable:`cross-compiling <CMAKE_CROSSCOMPILING>`.
  34. See policy :policy:`CMP0158`.
  35. * .. versionadded:: 3.29
  36. The target's :prop_tgt:`TEST_LAUNCHER`, if set, will be
  37. used to launch the command::
  38. <launcher> <command>
  39. If the :prop_tgt:`CROSSCOMPILING_EMULATOR` is also set, both are used::
  40. <launcher> <emulator> <command>
  41. The command may be specified using
  42. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  43. ``CONFIGURATIONS``
  44. Restrict execution of the test only to the named configurations.
  45. ``WORKING_DIRECTORY``
  46. Set the test property :prop_test:`WORKING_DIRECTORY` in which to execute the
  47. test. If not specified, the test will be run in
  48. :variable:`CMAKE_CURRENT_BINARY_DIR`. The working directory may be specified
  49. using :manual:`generator expressions <cmake-generator-expressions(7)>`.
  50. ``COMMAND_EXPAND_LISTS``
  51. .. versionadded:: 3.16
  52. Lists in ``COMMAND`` arguments will be expanded, including those created with
  53. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  54. If the test command exits with code ``0`` the test passes. Non-zero exit code
  55. is a "failed" test. The test property :prop_test:`WILL_FAIL` inverts this
  56. logic. Note that system-level test failures such as segmentation faults or
  57. heap errors will still fail the test even if ``WILL_FAIL`` is true. Output
  58. written to stdout or stderr is captured by :manual:`ctest(1)` and only
  59. affects the pass/fail status via the :prop_test:`PASS_REGULAR_EXPRESSION`,
  60. :prop_test:`FAIL_REGULAR_EXPRESSION`, or :prop_test:`SKIP_REGULAR_EXPRESSION`
  61. test properties.
  62. .. versionadded:: 3.16
  63. Added :prop_test:`SKIP_REGULAR_EXPRESSION` property.
  64. Example usage:
  65. .. code-block:: cmake
  66. add_test(NAME mytest
  67. COMMAND testDriver --config $<CONFIG>
  68. --exe $<TARGET_FILE:myexe>)
  69. This creates a test ``mytest`` whose command runs a ``testDriver`` tool
  70. passing the configuration name and the full path to the executable
  71. file produced by target ``myexe``.
  72. ---------------------------------------------------------------------
  73. The command syntax above is recommended over the older, less flexible form:
  74. .. code-block:: cmake
  75. add_test(<name> <command> [<arg>...])
  76. Add a test called ``<name>`` with the given command-line.
  77. Unlike the above ``NAME`` signature, target names are not supported
  78. in the command-line. Furthermore, tests added with this signature do not
  79. support :manual:`generator expressions <cmake-generator-expressions(7)>`
  80. in the command-line or test properties.