我在我的项目中使用CMake并设置了一个用于连续/夜间构建的cdash服务器。一切运作良好,通过设置crontab,我们将每小时/每晚的构建/测试结果自动上传到我们的cdash服务器。
我的下一步是将测试覆盖率报告添加到构建中。我在这里找到了这份文件http://www.cmake.org/Wiki/CTest:Coverage,但坦率地说,它与实用指南相差甚远。
目前我已添加了必需的标志(而不是-fprofile-arcs -ftest-coverage
,我发现--coverage
更好),编译过程会生成.gcno文件。但后来我被卡住了。命令
make NightlyCoverage
似乎没有做任何事情。谁能告诉我接下来要做什么?我想要的结果是make NightlyCoverage
,生成覆盖率报告并上传到cdash服务器。
答案 0 :(得分:45)
我一直在成功使用https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake。
遵循指南:将文件添加到我的CMAKE_MODULE_PATH
目录,添加
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
if(CMAKE_COMPILER_IS_GNUCXX)
include(CodeCoverage)
setup_target_for_coverage(${PROJECT_NAME}_coverage ${PROJECT_TEST_NAME} coverage)
endif()
在我的CMakeLists.txt
中。我还手动添加了gcov
作为我的目标的依赖项:
if(CMAKE_COMPILER_IS_GNUCXX)
target_link_libraries(${PROJECT_TEST_NAME} gcov)
endif()
有了这个,我只需输入
make my_project_coverage
我在构建树的coverage
目录中获取了html报告。
答案 1 :(得分:2)
我通过以下方式设置项目“ foo”。将cmake文件从https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake复制到子目录'cmake_modules'。在add_executable(foo ...)
之后的CMakeLists.txt文件中,添加了以下内容:
if(CMAKE_COMPILER_IS_GNUCXX)
LIST(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_modules")
include(CodeCoverage)
APPEND_COVERAGE_COMPILER_FLAGS()
set(COVERAGE_LCOV_EXCLUDES 'dir1/*' 'dir2/*') // this is optional if you want to exclude some directory from the report
SETUP_TARGET_FOR_COVERAGE_LCOV(NAME foo_coverage
EXECUTABLE foo
DEPENDENCIES foo)
endif()
cmake之后,建立目标 使 使foo_coverage 然后在build文件夹的foo_coverage文件夹中使用index.html文件打开报告
答案 2 :(得分:2)
此答案与@rcomblen的答案相同,但有点过时,因此我将分享我的解决方案。这是我所做的:
(base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ tree -L 2
.
├── CMakeLists.txt
├── cmake
│ └── CodeCoverage.cmake
├── src
│ ├── domath.cpp
│ ├── domath.h
│ └── testdomath.cpp
└── third_party
└── googletest
哪里
// src/domath.h
#ifndef COVERAGETEST_DOMATH_H
#define COVERAGETEST_DOMATH_H
class domath {
public:
int add(int a, int b);
};
#endif //COVERAGETEST_DOMATH_H
和
// src/domath.cpp
#include "domath.h"
int domath::add(int a, int b) {
return a + b;
}
和
// src/testdomath.cpp
#include "gtest/gtest.h"
#include "domath.h"
TEST(DoMathTests, testAdd){
domath m;
int actual = m.add(4, 6);
ASSERT_EQ(10, actual);
}
cmake/CodeCoverage.cmake
gcovr
。此步骤至关重要,因为此线程上的其他答案不再适用于我已经拥有的gcovr
版本:(base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ pip install gcovr
(base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ gcovr --version
gcovr 4.2
(base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ which gcovr
/home/ciaran/miniconda3/bin/gcovr
注意,对于cmake脚本,我们需要其中的输出gcovr
。
4)编写一个cmake脚本,该脚本创建一个库,一个测试可执行文件并使用CodeCoverage.cmake
模块:
cmake_minimum_required(VERSION 3.15)
project(CoverageTest)
set(CMAKE_CXX_STANDARD 14)
# setup googletest
add_subdirectory(third_party/googletest)
# create our library
add_library(DoMath STATIC src/domath.h src/domath.cpp)
add_dependencies(DoMath gtest gtest_main)
# create the test executable
add_executable(TestDoMath src/testdomath.cpp)
target_include_directories(TestDoMath PRIVATE third_party/googletest/googletest)
target_link_libraries(TestDoMath PRIVATE
DoMath gtest gtest_main)
add_dependencies(TestDoMath DoMath gtest gtest_main)
# now for coverage bits
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
if (CMAKE_COMPILER_IS_GNUCXX)
include(CodeCoverage)
append_coverage_compiler_flags()
# we need to turn off optimization for non-skewed coverage reports
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
# optional excludes - None needed here
# set(COVERAGE_EXCLUDES)
# importantly, set the path to the gcovr executable that you downladed
set(GCOVR_PATH "/home/ciaran/miniconda3/bin/gcovr")
# Works
setup_target_for_coverage_gcovr_xml(
NAME TestDoMathCoverageXml
EXECUTABLE TestDoMath
DEPENDENCIES TestDoMath DoMath
)
# Works
setup_target_for_coverage_gcovr_html(
NAME TestDoMathCoverageHtml
EXECUTABLE TestDoMath
DEPENDENCIES TestDoMath DoMath
)
# This one did not work for me:
setup_target_for_coverage_lcov(
NAME TestDoMathCoverageLcov
EXECUTABLE TestDoMath
DEPENDENCIES TestDoMath DoMath
)
endif ()
就这样。现在,只需建立新目标即可。
祝你好运。
答案 3 :(得分:1)
我有一个丑陋的方法,可以使用gcovr
制作 GCC代码覆盖率报告而不使用CodeCoverage.cmake
:移动所有*.gcno
和*.gcda
文件到项目根目录,然后运行gcovr
$ cd /path/to/your/project
$ mkdir build && cd build && cmake ..
$ make && make test
$ find . -type f \( -iname \*.gcno -or -iname \*.gcda \) -exec cp {} .. \;
$ cd ..
$ gcovr -v -r .