add_test.rst 2.7 KB

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