Avoid tests from subprojects being added to my project's list of tests

I have a project where I include Eigen like this:

find_package(Eigen3 CONFIG)
if(NOT Eigen3_FOUND)
    message(STATUS "Fetching Eigen3")
    FetchContent_Declare(Eigen3
        GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
        GIT_TAG "3147391d946bb4b6c68edd901f2add6ac1f31f8c"
    )
endif()

FetchContent_MakeAvailable(Eigen3)

target_include_directories(ql
    SYSTEM PRIVATE "${Eigen3_SOURCE_DIR}/Eigen"
    SYSTEM PRIVATE "${Eigen3_SOURCE_DIR}/unsupported"
)

target_link_libraries(my_project
    PRIVATE Eigen3::Eigen
)

if(MY_PROJECT_BUILD_TESTS)
    enable_testing()
    ...
)

If I have a look at Eigen’s library CMakeLists.txt, I see their tests are not guarded by something like EIGEN_BUILD_TESTS, but just by BUILD_TESTING:

option(BUILD_TESTING "Enable creation of Eigen tests." ON)
if(BUILD_TESTING)
  include(EigenConfigureTesting)

  if(EIGEN_LEAVE_TEST_IN_ALL_TARGET)
    add_subdirectory(test) # can't do EXCLUDE_FROM_ALL here, breaks CTest
  else()
    add_subdirectory(test EXCLUDE_FROM_ALL)
  endif()

  add_subdirectory(failtest)
endif()

So, when I run ctest I see all the Eigen tests are added.

Is there a way to avoid tests from suprojects, in this case Eigen, being added to my project’s list of tests?

You could set (BUILD_TESTING OFF) before FetchContent_MakeAvailable (Eigen3), but IMHO the best solution is to name all of your project’s test with a consistent prefix so that you can run only your tests with CTest’s name regex feature.

1 Like

Labels also work.

If you do want to scope things, Eigen probably doesn’t use PARENT_SCOPE, so with CMake 3.25+:

block (SCOPE_FOR VARIABLES)
  set(BUILD_TESTING OFF)
  FetchContent_MakeAvailable(Eigen3)
endblock ()

assuming that the MakeAvailable call doesn’t set variables you need at least…

1 Like

Thanks @benthevining and @ben.boeckel.

Yes, I actually had a working version using labels. I had a add_test and I just added the set_property line:

add_test(
    NAME "${name}"
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${workdir}"
    COMMAND "${name}"
)
set_property(TEST "${name}" PROPERTY LABELS TESTLABEL MyProjectTest)

Then ran ctest with: -L MyProjectTest.


Just as a side note, and for this particular case of Eigen, I’ve talked with the maintainers of the lib and they have it working fine in master via two different things:

  1. They only enable BUILD_TESTING if PROJECT_IS_TOP_LEVEL.
  2. They use a new EIGEN_BUILD_TESTING option.
1 Like