问题
I have a C++ project using cmake
as the build tool. My directory structure looks like the following.
. ├── cmake-build-debug ├── include ├── src └── tests
When I am inside cmake-build-debug
, I run cmake ..
followed by make clean && make && make test
. What I noticed is that only the first unit test is run. How do I configure my project so that when I run make test
, all unit tests are run? At the root, my CMakeLists.txt
looks like the following.
cmake_minimum_required(VERSION 3.10)
project(my_lib)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(src)
add_subdirectory(tests)
enable_testing()
My tests/CmakeLists.txt
looks like the following (configuration taken from this website).
find_package (Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
add_definitions (-DBOOST_TEST_DYN_LINK)
include_directories (../include ${Boost_INCLUDE_DIRS})
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
foreach(testSrc ${TEST_SRCS})
get_filename_component(testName ${testSrc} NAME_WE)
add_executable(${testName} ${testSrc})
target_link_libraries(${testName} ${Boost_LIBRARIES} my_lib)
set_target_properties(${testName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testBin)
add_test(NAME ${testName}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testBin
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/testBin/${testName}
)
endforeach(testSrc)
The test
target of cmake-build-debug/Makefile
that is generated looks like the following.
# Special rule for the target test
test:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running tests..."
/usr/bin/ctest --force-new-ctest-process $(ARGS)
.PHONY : test
# Special rule for the target test
test/fast: test
.PHONY : test/fast
The generated cmake-build-debug/CTestTestfile.cmake
looks like the following.
subdirs("src")
subdirs("tests")
The generated cmake-build-debug/src/CTestTestfile.cmake
is empty, but the cmake-build-debug/tests/CTestTestfile.cmake
has the following content. There are more test_XYZ.cpp
classes under the tests
directory, but they are not being added here.
add_test(test_Dummy "/path/to/git/my_lib/tests/testBin/test_Dummy")
set_tests_properties(test_Dummy PROPERTIES WORKING_DIRECTORY "/path/to/git/my_lib/tests/testBin")
Any ideas on what I am doing wrong?
回答1:
enable_testing()
enables add_test()
after it. So just make sure you call enable_testing()
before any add_test()
you want to enable.
来源:https://stackoverflow.com/questions/59976311/how-do-i-configure-my-cmake-project-to-run-all-unit-tests