I see that to add my google tests(for my cpp project), I need to make a call to enable_testing() in the root source directory. Can someone explain what this really does? Also why would cmake not make this default?
This is all I could get from the documentation.
Enables testing for this directory and below. See also the add_test() command. Note that ctest expects to find a test file in the build directory root. Therefore, this command should be in the source directory root.
enable_testing() Enables testing for this directory and below. This command should be in the source directory root because ctest expects to find a test file in the build directory root. This command is automatically invoked when the CTest module is included, except if the BUILD_TESTING option is turned off.
To write tests Therefore, you write and configure CTest tests just as you would in any CMake environment. Use the enable_testing() command to enable testing, and the add_test() or gtest_discover_tests() command to add a new test.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With