如何配置CMake项目以运行所有单元测试? [英] How do I configure my CMake project to run all unit tests?

查看:94
本文介绍了如何配置CMake项目以运行所有单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 cmake 作为构建工具的C ++项目.我的目录结构如下所示.

I have a C++ project using cmake as the build tool. My directory structure looks like the following.


.
├── cmake-build-debug
├── include
├── src
└── tests

当我进入 cmake-build-debug 时,我运行 cmake .. ,然后执行 make clean&&制作&&进行测试.我注意到的是,只有第一个单元测试才运行.如何配置我的项目,以便在运行 make test 时可以运行所有单元测试?从根开始,我的 CMakeLists.txt 如下所示.

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()

我的 tests/CmakeLists.txt 如下所示(此配置取自

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)

生成的 cmake-build-debug/Makefile test 目标如下所示.

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

生成的 cmake-build-debug/CTestTestfile.cmake 如下所示.

subdirs("src")
subdirs("tests")

生成的 cmake-build-debug/src/CTestTestfile.cmake 为空,但是 cmake-build-debug/tests/CTestTestfile.cmake 具有以下内容.在 tests 目录下还有更多的 test_XYZ.cpp 类,但此处未添加它们.

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?

推荐答案

enable_testing()之后启用 add_test().因此,只需确保在要启用的任何 add_test()之前先调用 enable_testing().

enable_testing() enables add_test() after it. So just make sure you call enable_testing() before any add_test() you want to enable.

这篇关于如何配置CMake项目以运行所有单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆