enable_testing.rst 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. enable_testing
  2. --------------
  3. Enables testing for the current directory and below:
  4. .. code-block:: cmake
  5. enable_testing()
  6. This command should be invoked in the top-level source directory because
  7. :manual:`ctest(1)` expects to find a test file in the top-level
  8. build directory.
  9. This command is also automatically invoked when the :module:`CTest`
  10. module is included, except if the :variable:`BUILD_TESTING`
  11. option is turned off.
  12. The following restrictions apply to where ``enable_testing()`` may be called:
  13. * It must be called in file scope, not in a :command:`function` call nor inside
  14. a :command:`block`.
  15. Examples
  16. ^^^^^^^^
  17. In the following example, this command is conditionally called depending on how
  18. the project is used. For instance, when the Example project is added via the
  19. :module:`FetchContent` module as a subdirectory of a parent project that defines
  20. its own tests, testing for the Example project is disabled.
  21. .. code-block:: cmake
  22. :caption: ``CMakeLists.txt``
  23. project(Example)
  24. option(Example_ENABLE_TESTING "Enable testing" ${PROJECT_IS_TOP_LEVEL})
  25. if(Example_ENABLE_TESTING)
  26. enable_testing()
  27. endif()
  28. # ...
  29. if(Example_ENABLE_TESTING)
  30. add_test(...)
  31. endif()
  32. See Also
  33. ^^^^^^^^
  34. * The :command:`add_test` command.