Testing

    Which will enable testing and set a option so users can turn testing on and off (along with a few other things). Or you can do this yourself by directly calling enable_testing().

    When you add your test folder, you should do something like this:

    1. if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
    2. add_subdirectory(tests)
    3. endif()

    The main use case for the override above is actually in this book’s own examples, as the master CMake project really does want to run all the subproject tests.

    You can register targets with:

      which would use the output location (thus, the executable) of the produced target.

      If you want to run CMake to build a project as part of a test, you can do that too (in fact, this is how CMake tests itself). For example, if your master project was called MyProject and you had an examples/simple project that could build by itself, this would look like:

      1. add_test(
      2. NAME
      3. ExampleCMakeBuild
      4. "${CMAKE_CTEST_COMMAND}"
      5. --build-and-test "${My_SOURCE_DIR}/examples/simple"
      6. --build-generator "${CMAKE_GENERATOR}"
      7. --test-command "${CMAKE_CTEST_COMMAND}"
      • : A popular option from Google. Development can be a bit slow.
      • Catch2: A modern, PyTest-like framework with clever macros.
      • : A replacement for Catch2 that is supposed to compile much faster and be cleaner. See Catch2 chapter and replace with DocTest.