[英]How do I configure my CMake project to run all unit tests?
I have a C++ project using cmake
as the build tool.我有一个使用
cmake
作为构建工具的 C++ 项目。 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
.当我在
cmake-build-debug
,我运行cmake ..
然后是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?如何配置我的项目,以便在运行
make test
时运行所有单元测试? At the root, my CMakeLists.txt
looks like the following.在根目录下,我的
CMakeLists.txt
如下所示。
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 ).我的
tests/CmakeLists.txt
如下所示(配置取自本网站)。
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.生成的
cmake-build-debug/Makefile
的test
目标如下所示。
# 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.生成的
cmake-build-debug/CTestTestfile.cmake
如下所示。
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.生成的
cmake-build-debug/src/CTestTestfile.cmake
是空的,但是cmake-build-debug/tests/CTestTestfile.cmake
有如下内容。 There are more test_XYZ.cpp
classes under the tests
directory, but they are not being added here.在
tests
目录下还有更多的test_XYZ.cpp
类,这里就不添加了。
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?关于我做错了什么的任何想法?
enable_testing()
enables add_test()
after it. enable_testing()
在它之后启用add_test()
。 So just make sure you call enable_testing()
before any add_test()
you want to enable.因此,请确保在要启用的任何
add_test()
之前调用enable_testing()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.