Using google tests with CMake/Ctest with the new command gtest_discover_tests

I am trying to use googletest with CMake/Ctest. I have several sources files for my tests (each one containing many TEST/TEST_F/... commands) which are located in several directories. I want that the tests related to a given source are executed in the same directory as their source file. Also, I prefer that the build process of a test source file is a test by itself. So I made something like:

file(GLOB_RECURSE test_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "tests/*.cpp")
foreach(test_src ${test_srcs}) get_filename_component(test_dir ${test_src} DIRECTORY) get_filename_component(test_exe ${test_src} )NAME_WE) add_executable(${test_exe} EXCLUDE_FROM_ALL tests/gtest_main.cpp ${test_src}) set_target_properties(${test_exe} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${test_dir} ) target_link_libraries(${test_exe} gtest) add_test(NAME build_${test_exe} COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${test_exe}) set_tests_properties(build_${test_exe} PROPERTIES FIXTURES_SETUP ${test_exe}) gtest_discover_tests(${test_exe} TEST_LIST list WORKING_DIRECTORY ${test_dir} PROPERTIES DEPENDS build_${test_exe} PROPERTIES FIXTURES_REQUIRED ${test_exe} )
endforeach()

But it seems that the dependencies I am trying to declare between the tests are not taken into account: the build of the tests does not necessarily occurs before the execution of the underlying tests...

If I use the old gtest_add_tests as in the following instead of gtest_discover_tests, it works:

gtest_add_tests( TARGET ${test_exe} SOURCES ${test_src} WORKING_DIRECTORY ${test_dir} TEST_LIST tlist )
set_tests_properties(${tlist} PROPERTIES FIXTURES_REQUIRED ${test_exe})

Am I missing something with gtest_discover_tests?

1

1 Answer

After having started the bounty, I re-started the research on my own. I found out, the simplest method out there is to have googletest installed system-wide.

So, first install the package. On Ubuntu 18.04, that was supt apt install googletest.

For some reason, I had to build the library (perhaps not necessary somehow though?):

cd /usr/src/googletest
mkdir bin && cd bin
cmake ..
make && make install

After that I have been able to compile and run a test case. My CMakeLists.txt testing section looks like this:

enable_testing()
find_package(GTest REQUIRED)
include(GoogleTest)
add_executable(tests tests/foo_test.cpp tests/bar_test.cpp)
target_link_libraries(tests GTest::GTest GTest::Main)
gtest_discover_tests(tests)

A minimal test case file looks like this in my project:

// tests/foo_test.cpp
#include "gtest/gtest.h"
TEST(Foo, Sum)
{ EXPECT_EQ(2, 1 + 1);
}

Compiling is as easy as:

mkdir bin && cd bin
cmake ..
./tests
5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like