add_test.rst 2.4 KB

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