In CMake's GoogleTest module, how do gtest_discover_tests and gtest_add_tests differ, and when does each one find the tests?
answer
- scan sources or ask the binary
- configure time versus build time
- parameterized names split or not
- cross-compiling needs an emulator
basics
~20 sgtest_add_tests finds tests at configure time by scanning sources with a regular expression, so a parameterized test stays one wildcard entry. gtest_discover_tests runs the built binary with --gtest_list_tests after the build or before testing, registering every instantiation.
solid answer
~50 sBoth register CTest tests that each run the binary with a `--gtest_filter` pattern: discovery creates one per GoogleTest test, the scan one per test macro it finds. **`gtest_add_tests`** (the older one) scans the target's sources with a regular expression **at configure time**: it always works when cross-compiling and lets you set per-test properties right away through `TEST_LIST`, but a `TEST_P` becomes a single test filtered by `*/Suite.Test/*`, unusual macro spellings can defeat the scan, and CMake must re-run to see new tests. **`gtest_discover_tests`** (CMake 3.10) runs the compiled binary with `--gtest_list_tests`, by default as a **post-build** step (`DISCOVERY_MODE POST_BUILD`) or just before `ctest` (`PRE_TEST`), so every instantiation gets its own test and no re-configure is needed. Its costs: a cross-compiled binary needs `CROSSCOMPILING_EMULATOR` or `PRE_TEST` mode, discovery has a 5-second `DISCOVERY_TIMEOUT` that fails the build, and per-test properties are set collectively through `PROPERTIES`. Use `XML_OUTPUT_DIR` for reports.
code
cmake · 15 linescmake_minimum_required(VERSION 3.25)
project(flaky CXX)
set(CMAKE_CXX_STANDARD 17) # GoogleTest 1.18 rejects anything older
set(CMAKE_CXX_STANDARD_REQUIRED ON)
enable_testing()
find_package(GTest REQUIRED)
include(GoogleTest)
add_executable(flaky_test flaky_test.cpp)
target_link_libraries(flaky_test PRIVATE GTest::gtest_main)
gtest_discover_tests(flaky_test XML_OUTPUT_DIR ${CMAKE_BINARY_DIR}/junit)
add_executable(flaky_test2 flaky_test.cpp)
target_link_libraries(flaky_test2 PRIVATE GTest::gtest_main)
gtest_add_tests(TARGET flaky_test2 TEST_PREFIX scan.)go deeper
Know that include(GoogleTest) and gtest_discover_tests turn each GoogleTest test into its own CTest test.
Contrast source scanning at configure time with listing the built binary, and what each does to parameterized tests.
Diagnose discovery timeouts and cross-compiling failures, and pick DISCOVERY_MODE, emulators or XML_OUTPUT_DIR accordingly.
Choose one registration approach for the codebase, weighing per-test processes and build-time discovery against configure-time scanning.
## Why the module exists CTest knows nothing about GoogleTest. Registering the binary once with `add_test` makes the whole suite one CTest test: one pass or fail, one timeout, no parallelism inside it. CMake's `GoogleTest` module, loaded with `include(GoogleTest)`, offers two commands that instead create **a CTest test per test they find**. Each generated test runs the binary with a `--gtest_filter` selecting its test, so `ctest -j` can run them in parallel and report them individually. The price is a process start per test, and per-process work such as a global environment's set-up repeats for every test. ## `gtest_add_tests`: scan the sources at configure time The older command reads the target's source files during `cmake` configuration and looks for `TEST`, `TEST_F`, `TEST_P`, `TYPED_TEST` and `TYPED_TEST_P` with a regular expression. - **Parameterized tests are not split.** It cannot know the instantiations, so `TEST_P(Sizes, Fits)` becomes one CTest test whose filter is `*/Sizes.Fits/*`; in the example it is listed as `scan.*/Sizes.Fits/*`. A typed test becomes `Suite/*.Test`. - **Re-configure to see changes.** By default it adds a dependency so editing a scanned source re-runs CMake; `SKIP_DEPENDENCY` turns that off. - **Fragile against unusual code.** Tests produced by a macro wrapper, or names built by the preprocessor, can be missed or mis-parsed. - **Strengths.** Tests exist at configure time, so `TEST_LIST` returns their names for `set_tests_properties`, and it always works when cross-compiling because nothing has to run. - `DISABLED_` tests are registered with the CTest `DISABLED` property and `--gtest_also_run_disabled_tests`, so CTest lists them as disabled. ## `gtest_discover_tests`: ask the binary Added in CMake 3.10, this command runs the compiled test executable with `--gtest_list_tests` and parses the answer. 1. **When it runs.** `DISCOVERY_MODE POST_BUILD`, the default, attaches discovery to the target's build, so a successful build includes running the binary once. `PRE_TEST` (CMake 3.18) delays discovery until `ctest` runs, on the machine that runs the tests. The `CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE` variable sets the default for every call. 2. **Every instantiation.** Because the binary lists its real tests, `INSTANTIATE_TEST_SUITE_P(Small, Sizes, Values(1, 2))` yields `Small/Sizes.Fits/1` and `Small/Sizes.Fits/2`. By default the trailing index is replaced by the parameter's printed value, and `NO_PRETTY_VALUES` / `NO_PRETTY_TYPES` keep GoogleTest's indices. 3. **No re-configure.** Adding a test only needs a rebuild. 4. **Timeout.** `DISCOVERY_TIMEOUT` defaults to 5 seconds. A binary with slow static initialisation, or one that cannot start because a shared library is missing, fails the **build** when discovery runs post-build. 5. **Cross-compiling.** A binary built for another architecture cannot run on the build host unless the target's `CROSSCOMPILING_EMULATOR` is set; `PRE_TEST` mode moves discovery to where the tests run. Useful options include `EXTRA_ARGS` (passed to every test), `TEST_FILTER` (CMake 3.22, a `--gtest_filter` applied during discovery against GoogleTest's original names), `PROPERTIES` for all discovered tests, `TEST_PREFIX`/`TEST_SUFFIX`, and `XML_OUTPUT_DIR` (CMake 3.18), which gives each test its own `--gtest_output=xml:` file rather than racing on one shared `EXTRA_ARGS` path under `ctest -j`. ## Choosing | Need | Better fit | |---|---| | Value- or type-parameterized tests listed individually | `gtest_discover_tests` | | Tests added without re-running CMake | `gtest_discover_tests` | | Cross-compiling with no emulator and no on-target CTest | `gtest_add_tests` | | Different properties on individual tests at configure time | `gtest_add_tests` (or a `TEST_INCLUDE_FILES` script) | GoogleTest's own CMake quickstart links `GTest::gtest_main` and calls `gtest_discover_tests`, which is the usual modern default. ## Failure stories to recognise - **The build fails after a harmless test change.** Post-build discovery ran the binary and it crashed or hung during start-up; the error is in test initialisation, not in compilation. - **A parameterized test fails but CTest names no instance.** The tests were registered by `gtest_add_tests`, so one wildcard CTest test covers every instantiation; switch to discovery to see which value failed. - **New tests never run in CI.** With `gtest_add_tests` and `SKIP_DEPENDENCY`, CMake was not re-run after the source changed. - **XML reports vanish or are truncated under `ctest -j`.** Several discovered tests wrote one shared `--gtest_output` path; use `XML_OUTPUT_DIR`. - **The suite is slower under CTest than when run directly.** Each GoogleTest test now pays a process start and any global set-up; that is the documented cost of per-test registration. The commands registering and running the tests, such as `add_test`, `ctest -R` or `ctest -j`, belong to CMake's CTest material.
- A cross-compiled ARM test binary breaks the x86 build at the gtest_discover_tests step. What are the options?Set the target's `CROSSCOMPILING_EMULATOR` (for example a QEMU user-mode wrapper) so CMake can run the binary on the build host, or pass `DISCOVERY_MODE PRE_TEST` so discovery waits until `ctest` runs on the target. Falling back to `gtest_add_tests` also works, at the cost of unsplit parameterized tests.
- Why might a build fail after adding a new test, with a message about test discovery timing out?With the default `POST_BUILD` mode, the build runs the binary with `--gtest_list_tests`, and discovery fails after `DISCOVERY_TIMEOUT`, 5 seconds by default. Slow static initialisers, a debugger hook or a missing runtime library can push it over. Raise `DISCOVERY_TIMEOUT`, fix the start-up cost, or move discovery to `PRE_TEST`.
- Believing gtest_add_tests registers each TEST_P instantiation separately
- Thinking gtest_discover_tests needs CMake re-run to pick up new tests
- Expecting gtest_discover_tests to work cross-compiled without an emulator or PRE_TEST
- Passing one --gtest_output path in EXTRA_ARGS with ctest -j
- Assuming discovered tests share one process and its global set-up