GoogleTest: Silent failure of dynamic test discovery in gtest_discover_tests()
CMake version: 3.22.3 (part of an embedded toolchain), reproducible on 3.28.20231205-g60fa02de
We have recently encountered an issue were GoogleTest-based unit-tests were not executed and reported as 'not built' / 'not run' by ctest:
$ ctest
Test project /build/cmake-gtest-repro
Start 1: repro-test_NOT_BUILT
Could not find executable repro-test_NOT_BUILT
Looked in the following places:
repro-test_NOT_BUILT
[...]]
Unable to find executable: repro-test_NOT_BUILT
1/1 Test #1: repro-test_NOT_BUILT .............***Not Run 0.00 sec
This happens because - in our scenario - _GOOGLETEST_DISCOVER_TESTS_SCRIPT
was undefined / out-of-scope when
gtest_discover_tests()
is called (see
here. The variable is defined here). Thus, this
essentially results in cmake -P ""
being called, with does nothing.
We do include(GoogleTest)
in a subdirectory that also provides helper functions to add new test cases (but not in the main CMakeLists.txt
). This causes the variable to be out-of-scope when those helper functions are called elsewhere. A minimal example to reproduce the issue is available at https://gitlab.kitware.com/moha/cmake-gtest-repro/.
I'll shortly file a PR with a proposal for a fix that is based on what Catch2 has done in their (very similar) ctest integration.
Note: cmake -P
(i.e. without the quotes) actually causes an error be reported. In older CMake
versions (for example 3.16), also -P ""
results in an error (as one would expect, I'd say):
CMake Error: No cmake script provided.
CMake Error: Problem processing arguments. Aborting.