1

我想使用 Gcov 报告使用 CMake 编译的静态库Catch测试套件的覆盖率。

.
├── CMakeLists.txt
├── bin
├── CMakeModules
│   └── CodeCoverage.cmake
├── src
│   ├── some_function.cpp
│   ├── another_function.cpp
│   └── library_name.hpp
└── test
    └── main.cpp

我按照此处的说明进行了添加CMakeModules/CodeCoverage.cmake(请参阅上面的文件系统树)。

# CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(my_project CXX)

set(CMAKE_CXX_STANDARD 11)

# Link CMake Gcov module
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMakeModules)
include(CodeCoverage.cmake)
append_coverage_compiler_flags()

# Library
file(GLOB_RECURSE LIBRARY_SOURCE_FILES src/*.cpp)
add_library(library STATIC ${LIBRARY_SOURCE_FILES})

# Tests
file(GLOB_RECURSE TEST_SOURCE_FILES test/*.cpp)
add_executable(test ${TEST_SOURCE_FILES})
target_link_libraries(test PRIVATE library)
set_target_properties(test
    PROPERTIES RUNTIME_OUTPUT_DIRECTORY
    ${CMAKE_SOURCE_DIR}/bin)

# Create a make target for the coverage executable
APPEND_COVERAGE_COMPILER_FLAGS()
SETUP_TARGET_FOR_COVERAGE_LCOV(
    NAME coverage
    EXECUTABLE bin/test
    DEPENDENCIES library test)

如果任何测试函数失败,二进制文件bin/test将返回一个非零值。这会导致coveragemake 目标失败:

$ make coverage

...

make[3]: *** [CMakeFiles/coverage.dir/build.make: CMakeFiles/coverage] Error 3
make[2]: *** [CMakeFiles/Makefile2: CMakeFiles/coverage.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2: CMakeFiles/coverage.dir/rule] Error 2

这些错误指向 Makefile 中调用测试二进制文件的行:

# build.make

CMakeFiles/coverage: bin/test
    bin/test

关于如何解决这个问题的任何建议?

4

1 回答 1

2

根据命令的执行,将子句的SETUP_TARGET_FOR_COVERAGE_LCOV全部内容传递给. 后者接受一个 shell 命令行,所以你可以创建一个命令行来运行你的测试但总是返回零。比如这样:EXECUTABLECOMMANDadd_custom_target

EXECUTABLE bin/test || /bin/true
于 2021-12-23T06:58:18.940 回答