0

When running ctest, I get the following error message:

Test project /home/path_to_my_project/build
No tests were found!!!

It is actually unclear what the error exactly means.

Following the official documentation, I call include(CTest) in the root folder, then I invoke:

if(BUILD_TESTING)
  add_subdirectory(test)
endif()

The tests files compile fine and end up in the my_project/build/subprojects/Build/my_project_core/test/testBin folder.

So my guess is that ctest does not find a file where it would expect it to find it, but the error message does not display any useful information to fix this error.

I think having some general context about how ctest work and what it expects would help users confronted to this error.

Code

Root:

# CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(foo)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subprojects)

set(STAGED_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/stage)

include(ExternalProject)
ExternalProject_Add(
  ${PROJECT_NAME}_core
  SOURCE_DIR
    ${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}_core
  CMAKE_ARGS
    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
    -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
    -DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}
  CMAKE_CACHE_ARGS
    -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
  BUILD_ALWAYS
    1
  INSTALL_COMMAND
    ""
  )

External project:

# external/upstream/CMakeLists.txt

add_subdirectory(boost)

Dummy boost external (does nothing if system version not found):

# external/upstream/boost/CMakeLists.txt

find_package(Boost ${Boost_MINIMUM_REQUIRED} QUIET CONFIG COMPONENTS "${BOOST_COMPONENTS_REQUIRED}")
if(Boost_FOUND)
  message(STATUS "Found Boost: ${_loc} (version ${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION})")
  add_library(boost_external INTERFACE)
else()
  message(STATUS "Boost ${Boost_MINIMUM_REQUIRED} could not be located, downloading and building Boost 1.72.0 instead.")
endif()

Core project:

# foo_core/CMakeLists.txt

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project("foo" LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(${PROJECT_NAME} INTERFACE)
# add alias so the project can be used with add_subdirectory
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

enable_testing()
add_subdirectory(test)

Tests:

# foo_core/test/CMakeLists.txt
cmake_minimum_required(VERSION 3.12)

# Include Boost as an imported target
find_package(Boost REQUIRED CONFIG COMPONENTS unit_test_framework)

# Keep test files in a separate source directory called test
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} unit_test/*.cpp)

#Run through each source
foreach(testSrc ${TEST_SRCS})
        #Extract the filename without an extension (NAME_WE)
        get_filename_component(testName ${testSrc} NAME_WE)

        #Add compile target
        add_executable(${testName} ${testSrc})

        #link to Boost libraries AND your targets and dependencies
        target_link_libraries(${testName} Boost::unit_test_framework )

        #I like to move testing binaries into a testBin directory
         set_target_properties(${testName} PROPERTIES
         RUNTIME_OUTPUT_DIRECTORY  ${CMAKE_BINARY_DIR}/test/testBin)

        target_include_directories(
          ${testName} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
                                  $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)

        #Finally add it to test execution - Notice the WORKING_DIRECTORY and COMMAND
        add_test(NAME ${testName}
                 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/testBin
                 COMMAND ${CMAKE_BINARY_DIR}/test/testBin/${testName} )
endforeach(testSrc)

foo_core/include/foo/bar.h

#ifndef __BAR_H_INCLUDED__
#define __BAR_H_INCLUDED__

template<class T>
T bar(T a, T b){ return a*b;};

#endif

foo_core/test/unit_test/foo_test.cpp

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE foo_test

#include <boost/test/unit_test.hpp>
namespace utf = boost::unit_test;

#include <foo/bar.h>

BOOST_AUTO_TEST_SUITE( bar_suite )

BOOST_AUTO_TEST_CASE( bar_case )
{
  BOOST_CHECK_EQUAL(bar(2,2), 4);
}

BOOST_AUTO_TEST_SUITE_END()
WaterFox
  • 850
  • 6
  • 18
  • 1
    This typically means no tests were *registered* with CTest. What does your CMake file in the `test` directory look like? Specifically, do you call `add_test()`? – Kevin Aug 10 '20 at 19:55
  • what does "no tests were registered with CTest" ? Does it mean we use the `add_test` command ? If yes, this command is called in a loop: `add_test(NAME ${testName} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/testBin COMMAND ${CMAKE_BINARY_DIR}/test/testBin/${testName} )` – WaterFox Aug 10 '20 at 19:58
  • 1
    Your project directory hierarchy suggests you have multiple sub-projects. If you're running `ctest` from the `/home/path_to_my_project/build` directory, it may not see tests registered as part of some *separate* CMake invocation (i.e. with build folder `/home/path_to_my_project/build/subprojects/Build/my_project_core`). It is hard to say why this error appears without knowing more of these specifics about your build process. Would it be possible to reduce your problem to a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Kevin Aug 10 '20 at 20:40
  • @squareskittles I will edit the question to fit the project structure. In the meantime, you can find a reproducible example here: https://github.com/Becheler/stackoverflow/tree/master/01 – WaterFox Aug 10 '20 at 22:36
  • Ty for the link. It was supposed to be an overnight momentary solution, but I moved out/in this week so I could not work on it. I pushed the repo yesterday by mistake as I was working on a minimal example to better ask the question. – WaterFox Aug 19 '20 at 13:01

1 Answers1

2

Add

ENABLE_TESTING()

before AddTest in subproject. You need to add this command not only in head Cmake project configuration but in subproject configuration file too.

NProg
  • 51
  • 5