Running tests for a library tries and fails to run tests for dependencies

My preference would be that if A depends upon B, then running the tests in A would not try to run the tests in B.

Here’s the CMakeList. THe one for the dependency lib is basically the same.

cmake_minimum_required(VERSION 3.22 FATAL_ERROR)

project(my_lib)

# ---- Dependencies ----

include(cmake/CPM.cmake)

if(NOT TARGET Catch)
    cpmaddpackage("gh:catchorg/Catch2@3.3.2")
endif()

if(NOT TARGET other_lib)
    cpmaddpackage(
        "https://git..../other-lib.git#main"
    )
endif()

# ---- Create library ----

add_library(${PROJECT_NAME} src/boot_jump.c src/boot_main.c)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PRIVATE src)
target_link_libraries(${PROJECT_NAME} PUBLIC other_lib)

# ---- Create test binary ----
set(PROJECT_TESTS ${PROJECT_NAME}_tests)
add_executable(${PROJECT_TESTS} test/boot_jump.test.cpp)
target_link_libraries(${PROJECT_TESTS} Catch2::Catch2WithMain
                      ${PROJECT_NAME})
target_compile_features(${PROJECT_TESTS} PRIVATE cxx_std_17)
target_include_directories(${PROJECT_TESTS} PRIVATE src)
target_compile_options(
    ${PROJECT_TESTS} PRIVATE "-g" "-gdwarf-2" "-Wno-c99-designator"
                             "-Wno-extern-c-compat")

# ---- Enable testing ----

list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(${PROJECT_TESTS})

The result when I run the tests looks like this:

cd build && ctest -C debug --output-on-failure
Test project .../my-project/build
    Start 1: Jump to valid bank
1/2 Test #1: Jump to valid bank ............................   Passed    0.03 sec
    Start 2: other_lib_tests_NOT_BUILT-b12d07c
Could not find executable other_lib_tests_NOT_BUILT-b12d07c
Looked in the following places:
other_lib_tests_NOT_BUILT-b12d07c
other_lib_tests_NOT_BUILT-b12d07c.exe
debug/other_lib_tests_NOT_BUILT-b12d07c
debug/other_lib_tests_NOT_BUILT-b12d07c.exe
debug/other_lib_tests_NOT_BUILT-b12d07c
debug/other_lib_tests_NOT_BUILT-b12d07c.exe
Unable to find executable: other_lib_tests_NOT_BUILT-b12d07c
2/2 Test #2: other_lib_tests_NOT_BUILT-b12d07c ...***Not Run   0.00 sec

50% tests passed, 1 tests failed out of 2

I’ve found a couple of workarounds that require that I deselect the tests I don’t want to run, but that feels like a band-aid. It feels like the way I’m setting up my tests is incorrect and causing these issues.

I’m struggling a bit to find examples of libraries that use catch2 for testing or especially libraries that depend upon other libs that both use catch2 for testing.

Any thoughts on what I’m doing wrong here?

If I’m not mistaken, CPM uses FetchContent under the hood.
FetchContent makes the dependencies become a part of your project, so there will be some things that will leak from the dependency to your project.
The only way I know to avoid what you’re experiencing is to disable testing for the dependency. Ideally the dependency provides an option to do so. Otherwise I sometimes had to patch the dependency.

Thanks, that’s a helpful lead. We own both of the libraries, so I can pretty quickly try something out.