What does enable_testing() do in cmake?

When you call add_test(...), CMake will not generate the tests unless enable_testing() has been called. Note that you usually don’t need to call this directly. Just include(CTest) and it will invoke it for you.

My CMake setup often looks like this:

include(CTest) # note: this adds a BUILD_TESTING which defaults to ON

# ...

if(BUILD_TESTING)
  add_subdirectory(tests)
endif()

In the tests directory:

# setup test dependencies
# googletest has some code they explain on how to set it up; put that here

add_executable(MyUnitTests
    # ...
)

target_link_libraries(MyUnitTests gtest_main)

add_test(MyUnitTestName MyUnitTests)

Leave a Comment

tech