# Differences in set\_property vs set\_tests\_properties for discovered gtest tests

**URL:** <https://discourse.cmake.org/t/differences-in-set-property-vs-set-tests-properties-for-discovered-gtest-tests/8080>\
**Category:** Code\
**Created:** [May 9, 2023, 1:34am UTC](https://discourse.cmake.org/t/differences-in-set-property-vs-set-tests-properties-for-discovered-gtest-tests/8080 "2023-05-09T01:34:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benschreiber](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benschreiber/32/3421_2.png) [@benschreiber](https://discourse.cmake.org/u/benschreiber)\
**Post date:** [May 9, 2023, 1:34am UTC](https://discourse.cmake.org/t/differences-in-set-property-vs-set-tests-properties-for-discovered-gtest-tests/8080/1 "2023-05-09T01:34:22Z")

</div>

My project builds a gtest binary. I have a set of test lists - text files of test names. If a test name is in list _foo_, I want to add label _foo_ to that discovered test. I can do this by adding a CTest script to TEST\_INCLUDE\_FILES.

Properties can be set on tests with set\_property or set\_tests\_properties. I want to use set\_property because it has an APPEND form. If a test is in multiple lists, I want to add multiple labels, and it is simplest to do this with multiple loops. Unfortunately, set\_property(TEST test …) always fails on gtest discovered tests with “set\_property given TEST names that do not exist”.

Conversely, set\_tests\_properties says in its documentation “If the test is not found, CMake will report an error,” but this is not true. set\_tests\_properties() can be called for anything in the CTest script, and invalid names will be ignored.

My guess is that some exception was added for set\_tests\_properties to work on gtest discovered tests before they really exist, and this exception does not apply to set\_property. But this is pure speculation.

Is this the intended behavior? Ideally, I want to be able to use set\_property and its append form, and I also don’t want errors for test names that aren’t found.

Quick reproduction:  
hello\_test.cc

```cpp
#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
  // Expect two strings not to be equal.
  EXPECT_STRNE("hello", "world");
  // Expect equality.
  EXPECT_EQ(7 * 6, 42);
}

```

CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.26)
project(repro)

set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(hello_test
    hello_test.cc
)
target_link_libraries(hello_test GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(hello_test)

set_property(DIRECTORY APPEND PROPERTY
TEST_INCLUDE_FILES ${CMAKE_CURRENT_LIST_DIR}/LabelTests.cmake
)

```

LabelTests.cmake

```auto
set_tests_properties("HelloTest.BasicAssertions" PROPERTIES LABELS foo)
set_tests_properties("fake-test" PROPERTIES LABELS foo)

set_property(TEST "HelloTest.BasicAssertions" APPEND PROPERTY LABELS bar)

```

---

<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:** [May 9, 2023, 10:39pm UTC](https://discourse.cmake.org/t/differences-in-set-property-vs-set-tests-properties-for-discovered-gtest-tests/8080/2 "2023-05-09T22:39:06Z")

</div>

I am not 100% certain, but I believe you can’t call `set_property()` in a script run at test time. It expects the full internal data available only at configure time. The `set_tests_properties()` command is supported at test time though. I don’t know the details, but I’ve hit a situation that sounds similar to yours in the past, and that’s my recollection of what I found back then.

---

<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:** [May 10, 2023, 11:00am UTC](https://discourse.cmake.org/t/differences-in-set-property-vs-set-tests-properties-for-discovered-gtest-tests/8080/3 "2023-05-10T11:00:21Z")

</div>

> [@craig.scott](#):
>
> I don’t know the details, but I’ve hit a situation that sounds similar to yours in the past, and that’s my recollection of what I found back then.

The “detail” is that CTest reimplements the minimum bits for the commands that show up in the generated CTest scripts; they [are not the CMake commands of the same name](https://gitlab.kitware.com/cmake/cmake/-/blob/65430031a61105f9c8bae57e3731fe133e931b11/Source/CTest/cmCTestTestHandler.cxx#L1789-1806).
