How to properly use TEST_XML_OUTPUT_DIR?

I’m writing a CI script for an existing repository.

I would like to simply run all the gtest-based unit tests with ctest (in parallel, if possible) and store the summary into one or more xml files, so that the CI system knows where to look for them and can display the results in some user-friendly way.

I’ve seen https://github.com/google/googletest/issues/2506 which points to the recently added TEST_XML_OUTPUT_DIR in commit https://gitlab.kitware.com/cmake/cmake/-/commit/e9ab39eb1db8f072b030a929992df828bc05cc63, but I struggle to understand how to use this without the need to change the CMakeLists.txt itself. According to my understanding this variable needs to be added to gtest_discover_tests()? Is this really the only way?

I mean: In the same way as I wouldn’t want to hardcode /home/penguin/mycoolnewicecreamproject/build/x86_64-linux-clang inside CMakeLists.txt as the directory where the project is supposed to be built on my own machine, I wouldn’t want to hardcode the directory where I want to have my google tests results either. (I don’t want to hardcode that the results should be in xml format either.)

I tried a couple of things in the command-line, but I didn’t get any of those working according to my expectations.

Yes, the CMakeLists.txt would need to be modified. You could do something like this though:

set(gtest_xml_output_args)
if (NOT "$ENV{CI}" STREQUAL "")
  list(APPEND gtest_xml_output_args
    TEST_XML_OUTPUT_DIR "${CMAKE_BINARY_DIR}/output")
endif ()
gtest_discover_tests(… ${gtest_xml_output_args})

Alternatively, do if (DEFINED CI_XML_OUTPUT_DIR) and use it instead by passing -DCI_XML_OUTPUT_DIR during your CI instructions.