add_test.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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` or
  33. :prop_test:`FAIL_REGULAR_EXPRESSION` test property is used.
  34. The ``COMMAND`` and ``WORKING_DIRECTORY`` options may use "generator
  35. expressions" with the syntax ``$<...>``. See the
  36. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  37. Example usage:
  38. .. code-block:: cmake
  39. add_test(NAME mytest
  40. COMMAND testDriver --config $<CONFIGURATION>
  41. --exe $<TARGET_FILE:myexe>)
  42. This creates a test ``mytest`` whose command runs a ``testDriver`` tool
  43. passing the configuration name and the full path to the executable
  44. file produced by target ``myexe``.
  45. .. note::
  46. CMake will generate tests only if the :command:`enable_testing`
  47. command has been invoked. The :module:`CTest` module invokes the
  48. command automatically unless the ``BUILD_TESTING`` option is turned
  49. ``OFF``.
  50. ---------------------------------------------------------------------
  51. .. code-block:: cmake
  52. add_test(<name> <command> [<arg>...])
  53. Add a test called ``<name>`` with the given command-line. Unlike
  54. the above ``NAME`` signature no transformation is performed on the
  55. command-line to support target names or generator expressions.