# How can you use the cmake file api to determine whether a test is defined for a target ?

**URL:** https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191
**Category:** Usage
**Created:** [September 23, 2025, 1:26pm UTC](https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191 "2025-09-23T13:26:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rhabacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rhabacker/32/5828_2.png) [@rhabacker](https://discourse.cmake.org/u/rhabacker)
#### Post date: [September 23, 2025, 1:26pm UTC](https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191/1 "2025-09-23T13:26:35Z")

</div>

A problem has been identified in Qt Creator that causes running or debugging targets to always use the launcher defined with CMAKE\_TEST\_LAUNCHER, see [https://bugreports.qt.io/browse/QTCREATORBUG-32550](https://bugreports.qt.io/browse/QTCREATORBUG-32550).

The current workaround is to check the CTestTestfile.cmake file generated in the top-level build directory to see if a test exists for a specific target and, if not, not to use the launcher for that target, see [https://codereview.qt-project.org/c/qt-creator/qt-creator/+/676306](https://codereview.qt-project.org/c/qt-creator/qt-creator/+/676306)

Since CTestTestfile.cmake files can also be contained in subdirectories, this solution seems incomplete, and the question is whether there is a better way to achieve this via the cmake file api.

---

<div class="post-metadata">

### Author: ![rhabacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rhabacker/32/5828_2.png) [@rhabacker](https://discourse.cmake.org/u/rhabacker)
#### Post date: [September 24, 2025, 7:08am UTC](https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191/2 "2025-09-24T07:08:52Z")

</div>

> [@rhabacker](#):
>
> and the question is whether there is a better way to achieve this via the cmake file api.

The documentation for the cmake file API “[https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html#manual:cmake-file-api”](https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html#manual:cmake-file-api%E2%80%9D) does not mention that cmake tests are exported and which targets are used by tests.

---

<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: [September 24, 2025, 12:24pm UTC](https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191/3 "2025-09-24T12:24:27Z")

</div>

Information about tests can’t be provided via the CMake file API. The set of tests may not be known until after a build completes. For example, the `gtest_discover_tests()` command from the `GoogleTest` module runs the executable to ask it what test it defines. This can happen at build time or be delayed as late as when tests are run.

Because of the above, the only way you can reliably get information about tests is to run `ctest`. You can use the `--dry-run` option to at least query the set of tests without running them, but note that may still run executables to ask them to enumerate the tests they provide.

---

<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: [September 24, 2025, 2:36pm UTC](https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191/4 "2025-09-24T14:36:52Z")

</div>

This has been raised as [CMake Issue 27259](https://gitlab.kitware.com/cmake/cmake/-/issues/27259).

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [September 24, 2025, 9:27pm UTC](https://discourse.cmake.org/t/how-can-you-use-the-cmake-file-api-to-determine-whether-a-test-is-defined-for-a-target/15191/5 "2025-09-24T21:27:55Z")

</div>

The question was not about what tests are combined in one binary. The question was what built executables are used in an add\_test(). And this is also the scope of the test launcher that this question is about.
