Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does enable_testing() do in cmake?

Tags:

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.

like image 485
informativeguy Avatar asked May 22 '18 13:05

informativeguy


People also ask

What is enable_testing in CMake?

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.

How to add test in CTest?

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.


1 Answers

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) 
like image 93
Justin Avatar answered Oct 22 '22 16:10

Justin