add_test.rst 2.7 KB

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