# Wrapping tests discovered by gtest\_discover\_tests into a wrapper.

**URL:** https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852
**Category:** Code
**Created:** [January 15, 2024, 9:57am UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852 "2024-01-15T09:57:31Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![solemnPonie0](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@solemnPonie0](https://discourse.cmake.org/u/solemnPonie0)
#### Post date: [January 15, 2024, 9:57am UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/1 "2024-01-15T09:57:31Z")

</div>

Hello, everyone.

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.

Any ideas how to do it better?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 15, 2024, 9:25pm UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/2 "2024-01-15T21:25:10Z")

</div>

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:

[https://gitlab.kitware.com/cmake/cmake/-/merge\_requests/8963](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8963)

@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?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [January 16, 2024, 1:36pm UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/3 "2024-01-16T13:36:07Z")

</div>

> [@craig.scott](#):
>
> perhaps that property should also be used when obtaining the test list

Please open an issue to propose that.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 16, 2024, 9:55pm UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/4 "2024-01-16T21:55:48Z")

</div>

Done, new issue created here: [https://gitlab.kitware.com/cmake/cmake/-/issues/25603](https://gitlab.kitware.com/cmake/cmake/-/issues/25603)

---

<div class="post-metadata">

### Author: ![mateusz.patyk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mateusz.patyk](https://discourse.cmake.org/u/mateusz.patyk)
#### Post date: [December 19, 2025, 10:49am UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/5 "2025-12-19T10:49:06Z")

</div>

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:

```cmake
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?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 19, 2025, 8:51pm UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/6 "2025-12-19T20:51:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mateusz.patyk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mateusz.patyk](https://discourse.cmake.org/u/mateusz.patyk)
#### Post date: [December 29, 2025, 10:43am UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/7 "2025-12-29T10:43:31Z")

</div>

What kind of wrapper did you have in mind? A wrapper around `gtest_discover_tests()`, or is there a way to alter its behavior?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 29, 2025, 9:18pm UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/8 "2025-12-29T21:18:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mateusz.patyk](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mateusz.patyk](https://discourse.cmake.org/u/mateusz.patyk)
#### Post date: [December 30, 2025, 10:49pm UTC](https://discourse.cmake.org/t/wrapping-tests-discovered-by-gtest-discover-tests-into-a-wrapper/9852/9 "2025-12-30T22:49:23Z")

</div>

Ahhh, now I understand… Indeed. A simple solution, yet effective. Thanks!
