add_test.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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`. The options are:
  12. ``COMMAND``
  13. Specify the test command-line. If ``<command>`` specifies an
  14. executable target (created by :command:`add_executable`) it will
  15. automatically be replaced by the location of the executable created
  16. at build time.
  17. The command may be specified using
  18. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  19. ``CONFIGURATIONS``
  20. Restrict execution of the test only to the named configurations.
  21. ``WORKING_DIRECTORY``
  22. Set the :prop_test:`WORKING_DIRECTORY` test property to
  23. specify the working directory in which to execute the test.
  24. If not specified the test will be run with the current working
  25. directory set to the build directory corresponding to the
  26. current source directory.
  27. The working directory may be specified using
  28. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  29. ``COMMAND_EXPAND_LISTS``
  30. .. versionadded:: 3.16
  31. Lists in ``COMMAND`` arguments will be expanded, including those
  32. created with
  33. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  34. The given test command is expected to exit with code ``0`` to pass and
  35. non-zero to fail, or vice-versa if the :prop_test:`WILL_FAIL` test
  36. property is set. Any output written to stdout or stderr will be
  37. captured by :manual:`ctest(1)` but does not affect the pass/fail status
  38. unless the :prop_test:`PASS_REGULAR_EXPRESSION`,
  39. :prop_test:`FAIL_REGULAR_EXPRESSION` or
  40. :prop_test:`SKIP_REGULAR_EXPRESSION` test property is used.
  41. .. versionadded:: 3.16
  42. Added :prop_test:`SKIP_REGULAR_EXPRESSION` property.
  43. Tests added with the ``add_test(NAME)`` signature support using
  44. :manual:`generator expressions <cmake-generator-expressions(7)>`
  45. in test properties set by :command:`set_property(TEST)` or
  46. :command:`set_tests_properties`.
  47. Example usage:
  48. .. code-block:: cmake
  49. add_test(NAME mytest
  50. COMMAND testDriver --config $<CONFIG>
  51. --exe $<TARGET_FILE:myexe>)
  52. This creates a test ``mytest`` whose command runs a ``testDriver`` tool
  53. passing the configuration name and the full path to the executable
  54. file produced by target ``myexe``.
  55. .. note::
  56. CMake will generate tests only if the :command:`enable_testing`
  57. command has been invoked. The :module:`CTest` module invokes the
  58. command automatically unless the ``BUILD_TESTING`` option is turned
  59. ``OFF``.
  60. ---------------------------------------------------------------------
  61. This command also supports a simpler, but less flexible, signature:
  62. .. code-block:: cmake
  63. add_test(<name> <command> [<arg>...])
  64. Add a test called ``<name>`` with the given command-line.
  65. Unlike the above ``NAME`` signature, target names are not supported
  66. in the command-line. Furthermore, tests added with this signature do not
  67. support :manual:`generator expressions <cmake-generator-expressions(7)>`
  68. in the command-line or test properties.