So, gtest_discover_tests() runs my gtest-test --gtest_list_tests, and defines a separate ctest test for each result.
Now I want to run those tests wrapped in a script. That is, instead of running gtest-test --gtest_list_tests , I want ctest to run my_wrapper.sh gtest-test --gtest_list_tests .
Presumably, in the wrapper I would do something like
perf record --output=/tmp/whatever “$@”
I guess, I can do gtest_discover_tests() with my wrapper, and pass the actual test executable in EXTRA_ARGS, and make my own scanner for the presence of --gtest_list_tests and other args in the non-extra args, but it might be a little crutchy.
There is a change on CMake master already which adds a new TEST_LAUNCHER test property and corresponding CMAKE_TEST_LAUNCHER variable. It will appear in CMake 3.29.0, and the release cycle for that will probably start in a couple of weeks. One could mount an argument that perhaps that property should also be used when obtaining the test list by running some-exe --gtest_list_tests. Here’s the relevant change:
@brad.king It would be considerably simpler to decide whether this should be included in the test discovery step of gtest_discover_tests() before the 3.29.0 release than to try to change that behavior later with a policy. If we put a change in now to make the test discovery use the launcher as well, then that means we can say from the beginning that it is the launcher’s responsibility to handle it. Thoughts?
I have a slightly different use case. I want to call gtest_discover_tests() without the TEST_LAUNCHER property, but use TEST_LAUNCHER when running the tests. Is that possible?
I have something like this:
include(GoogleTest)
add_executable(${TEST_PROGRAM})
set_target_properties(${TEST_PROGRAM} PROPERTIES TEST_LAUNCHER "valgrind;--tool=memcheck;--error-exitcode=1;--leak-check=full") # NOTE: Don't use it for gtest_discover_tests() but for actual test running
gtest_discover_tests(${TEST_PROGRAM})
If I want to wrap the tests with Valgrind and there are issues (such as memory leaks), gtest_discover_tests() will fail to list the tests. Therefore, I would like to disable TEST_LAUNCHER for gtest_discover_tests() but enable it when running the tests. Is this possible?
In that case, I’d create my own wrapper that checks for whether --gtest_list_tests was included in the command arguments and decide whether to prepend the real launcher or not based on that.
I meant the wrapper script to be used as the TEST_LAUNCHER. Rather than putting valgrind there directly right now, have your wrapper script run the test executable via valgrind only if not doing test discovery. If you see a --gtest_list_tests in the command arguments, run the executable directly instead of via valgrind.