add_test.rst 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. ``CONFIGURATIONS``
  18. Restrict execution of the test only to the named configurations.
  19. ``WORKING_DIRECTORY``
  20. Set the :prop_test:`WORKING_DIRECTORY` test property to
  21. specify the working directory in which to execute the test.
  22. If not specified the test will be run with the current working
  23. directory set to the build directory corresponding to the
  24. current source directory.
  25. ``COMMAND_EXPAND_LISTS``
  26. .. versionadded:: 3.16
  27. Lists in ``COMMAND`` arguments will be expanded, including those
  28. created with
  29. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  30. The given test command is expected to exit with code ``0`` to pass and
  31. non-zero to fail, or vice-versa if the :prop_test:`WILL_FAIL` test
  32. property is set. Any output written to stdout or stderr will be
  33. captured by :manual:`ctest(1)` but does not affect the pass/fail status
  34. unless the :prop_test:`PASS_REGULAR_EXPRESSION`,
  35. :prop_test:`FAIL_REGULAR_EXPRESSION` or
  36. :prop_test:`SKIP_REGULAR_EXPRESSION` test property is used.
  37. .. versionadded:: 3.16
  38. Added :prop_test:`SKIP_REGULAR_EXPRESSION` property.
  39. The ``COMMAND`` and ``WORKING_DIRECTORY`` options may use "generator
  40. expressions" with the syntax ``$<...>``. See the
  41. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  42. Example usage:
  43. .. code-block:: cmake
  44. add_test(NAME mytest
  45. COMMAND testDriver --config $<CONFIGURATION>
  46. --exe $<TARGET_FILE:myexe>)
  47. This creates a test ``mytest`` whose command runs a ``testDriver`` tool
  48. passing the configuration name and the full path to the executable
  49. file produced by target ``myexe``.
  50. .. note::
  51. CMake will generate tests only if the :command:`enable_testing`
  52. command has been invoked. The :module:`CTest` module invokes the
  53. command automatically unless the ``BUILD_TESTING`` option is turned
  54. ``OFF``.
  55. ---------------------------------------------------------------------
  56. .. code-block:: cmake
  57. add_test(<name> <command> [<arg>...])
  58. Add a test called ``<name>`` with the given command-line. Unlike
  59. the above ``NAME`` signature no transformation is performed on the
  60. command-line to support target names or generator expressions.