add_test.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. If ``<command>`` specifies an executable
  23. target created by :command:`add_executable`, it will automatically be
  24. replaced by the location of the executable created at build time.
  25. The command may be specified using
  26. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  27. ``CONFIGURATIONS``
  28. Restrict execution of the test only to the named configurations.
  29. ``WORKING_DIRECTORY``
  30. Set the test property :prop_test:`WORKING_DIRECTORY` in which to execute the
  31. test. If not specified, the test will be run in
  32. :variable:`CMAKE_CURRENT_BINARY_DIR`. The working directory may be specified
  33. using :manual:`generator expressions <cmake-generator-expressions(7)>`.
  34. ``COMMAND_EXPAND_LISTS``
  35. .. versionadded:: 3.16
  36. Lists in ``COMMAND`` arguments will be expanded, including those created with
  37. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  38. If the test command exits with code ``0`` the test passes. Non-zero exit code
  39. is a "failed" test. The test property :prop_test:`WILL_FAIL` inverts this
  40. logic. Note that system-level test failures such as segmentation faults or
  41. heap errors will still fail the test even if ``WILL_FALL`` is true. Output
  42. written to stdout or stderr is captured by :manual:`ctest(1)` and only
  43. affects the pass/fail status via the :prop_test:`PASS_REGULAR_EXPRESSION`,
  44. :prop_test:`FAIL_REGULAR_EXPRESSION`, or :prop_test:`SKIP_REGULAR_EXPRESSION`
  45. test properties.
  46. .. versionadded:: 3.16
  47. Added :prop_test:`SKIP_REGULAR_EXPRESSION` property.
  48. Example usage:
  49. .. code-block:: cmake
  50. add_test(NAME mytest
  51. COMMAND testDriver --config $<CONFIG>
  52. --exe $<TARGET_FILE:myexe>)
  53. This creates a test ``mytest`` whose command runs a ``testDriver`` tool
  54. passing the configuration name and the full path to the executable
  55. file produced by target ``myexe``.
  56. ---------------------------------------------------------------------
  57. The command syntax above is recommended over the older, less flexible form:
  58. .. code-block:: cmake
  59. add_test(<name> <command> [<arg>...])
  60. Add a test called ``<name>`` with the given command-line.
  61. Unlike the above ``NAME`` signature, target names are not supported
  62. in the command-line. Furthermore, tests added with this signature do not
  63. support :manual:`generator expressions <cmake-generator-expressions(7)>`
  64. in the command-line or test properties.