# Howto check for existence of test in sub directory?

**URL:** https://discourse.cmake.org/t/howto-check-for-existence-of-test-in-sub-directory/7416
**Category:** Usage
**Tags:** tool:ctest
**Created:** [February 7, 2023, 7:09am UTC](https://discourse.cmake.org/t/howto-check-for-existence-of-test-in-sub-directory/7416 "2023-02-07T07:09:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![georg](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@georg](https://discourse.cmake.org/u/georg)
#### Post date: [February 7, 2023, 7:09am UTC](https://discourse.cmake.org/t/howto-check-for-existence-of-test-in-sub-directory/7416/1 "2023-02-07T07:09:16Z")

</div>

Hi all,

I need to check if a test with a certain name already exists.

The obvious way is the following:

```auto
add_test(NAME my_test COMMAND /bin/true)

if(TEST my_test)
    message(STATUS "yes")
else()
   message(STATUS "no")
endif()

```

This works for tests added in the same directory, but not if the test was added in a sub directory. I have tested it with CMake 3.22 and CMake 3.23.

Please see this [github repo](https://github.com/sercxanto/cmake_playground/tree/main/check_test_existence) where I set up an example.

Shouldn’t test names be global? How can I check for tests defined in any directory?

Regards  
Georg

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 8, 2023, 5:05pm UTC](https://discourse.cmake.org/t/howto-check-for-existence-of-test-in-sub-directory/7416/2 "2023-02-08T17:05:21Z")

</div>

It doesn’t look like test names are global at all. I really don’t know what happens if you have overlaps though…

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 8, 2023, 5:09pm UTC](https://discourse.cmake.org/t/howto-check-for-existence-of-test-in-sub-directory/7416/3 "2023-02-08T17:09:17Z")

</div>

Looks like it’s fine to have the same test name in different directories:

```auto
Test project …/cm-test-collide/build
    Start 1: duplicate
1/3 Test #1: duplicate ........................ Passed 0.00 sec
    Start 2: duplicate
2/3 Test #2: duplicate ........................ Passed 0.00 sec
    Start 3: duplicate
3/3 Test #3: duplicate ........................ Passed 0.00 sec

100% tests passed, 0 tests failed out of 3

```

---

<div class="post-metadata">

### Author: ![georg](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/9f8e36/32.png) [@georg](https://discourse.cmake.org/u/georg)
#### Post date: [February 13, 2023, 4:09pm UTC](https://discourse.cmake.org/t/howto-check-for-existence-of-test-in-sub-directory/7416/4 "2023-02-13T16:09:30Z")

</div>

In the meanwhile I managed to solve my original problem in another way, so I don’t need to check for the existence of tests anymore.

Thanks for having at look at it, Ben. The behavior seems a bit strange at first as I

- didn’t expect multiple tests with the same name to be valid and
- did expect that `if(TEST ...)` works globally or at least catches the same tests as ctest would do on a directory level (tests defined in the current level and below)

Perhaps the documentation could be improved at that point.

However for me its solved, as I don’t need it anymore.
