Skip to content

Fix catch_discover_tests PRE_TEST failure with zero discoverable tests#3075

Open
solarispika wants to merge 1 commit intocatchorg:develfrom
solarispika:fix/pretest-zero-tests-missing-file
Open

Fix catch_discover_tests PRE_TEST failure with zero discoverable tests#3075
solarispika wants to merge 1 commit intocatchorg:develfrom
solarispika:fix/pretest-zero-tests-missing-file

Conversation

@solarispika
Copy link

Summary

When using catch_discover_tests() with DISCOVERY_MODE PRE_TEST and a multi-config generator (e.g., Ninja Multi-Config), if a test target has zero discoverable tests (e.g., all tests tagged with [.]), ctest fails with:

CMake Error at .../test-hidden-b12d07c_include-Release.cmake:26 (include):
  include could not find requested file:
    .../test-hidden-b12d07c_tests-Release.cmake

This error aborts all test discovery, preventing any tests from running — even from other targets that have tests.

Root Cause

The early return added in #2962 (76f70b1) correctly prevented a JSON parsing crash for zero tests, but skipped file(WRITE "${_CTEST_FILE}" ...). The PRE_TEST code path in Catch.cmake unconditionally include()s this file (line 282), so the missing file causes a hard error.

The POST_BUILD code path is unaffected because it wraps the include in if(EXISTS ...).

Fix

Write an empty file before the early return so the include() always succeeds.

Reproduction

Minimal project with two test targets — one normal, one with only hidden ([.]) tests:

CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(catch2-pretest-bug LANGUAGES CXX)

include(FetchContent)
FetchContent_Declare(
    Catch2
    GIT_REPOSITORY https://github.com/catchorg/Catch2.git
    GIT_TAG v3.13.0
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(Catch)

enable_testing()
set(CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)

add_executable(test-normal test-normal.cpp)
target_link_libraries(test-normal PRIVATE Catch2::Catch2WithMain)
catch_discover_tests(test-normal)

add_executable(test-hidden test-hidden.cpp)
target_link_libraries(test-hidden PRIVATE Catch2::Catch2WithMain)
catch_discover_tests(test-hidden)
test-normal.cpp
#include <catch2/catch_test_macros.hpp>

TEST_CASE("Normal test") {
    REQUIRE(1 + 1 == 2);
}
test-hidden.cpp
#include <catch2/catch_test_macros.hpp>

// All tests are hidden (tagged with [.]), so --list-tests returns zero results.
TEST_CASE("Hardware-dependent test", "[.DUT]") {
    REQUIRE(true);
}
CMakePresets.json
{
    "version": 6,
    "configurePresets": [
        {
            "name": "default",
            "generator": "Ninja Multi-Config",
            "binaryDir": "${sourceDir}/build"
        }
    ],
    "buildPresets": [
        {
            "name": "release",
            "configurePreset": "default",
            "configuration": "Release"
        }
    ]
}
cmake --preset default
cmake --build --preset release
ctest --test-dir build --build-config Release
# Error: include could not find requested file: .../test-hidden-b12d07c_tests-Release.cmake

After this fix, test-normal runs and passes; test-hidden is gracefully skipped.

When using catch_discover_tests() with DISCOVERY_MODE PRE_TEST and a
multi-config generator (e.g. Ninja Multi-Config), if a test target has
zero discoverable tests (e.g. all tests tagged with [.]), ctest fails:

  CMake Error: include could not find requested file:
    .../test-hidden-b12d07c_tests-Release.cmake

The early return added in catchorg#2962 (76f70b1) correctly prevented a JSON
parsing crash for zero tests, but skipped writing the ctest file. The
PRE_TEST include script unconditionally includes this file, so the
missing file causes a hard error that aborts all test discovery.

Write an empty file before returning early so the include always
succeeds.
@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.37%. Comparing base (50e9dbf) to head (2bc2f3b).

Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #3075      +/-   ##
==========================================
- Coverage   91.38%   91.37%   -0.01%     
==========================================
  Files         204      204              
  Lines        8899     8899              
==========================================
- Hits         8132     8131       -1     
- Misses        767      768       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant