我正在使用lcov为我正在工作的项目生成覆盖率信息。它大部分都在工作,除非出于某些原因,它似乎没有在每一个单独的情况下计算函数的背线。这意味着拥有完整测试覆盖率的类文件仍然会缺少几行,因此显示为90%。
这没什么大不了的,但有点让人讨厌。我想知道有没有人知道为什么。
下面我已经提供了一个完整的最小示例来演示这个问题。当这个程序运行时,6行中有4行被“命中”,留下了68.7%的行覆盖率,尽管所有行都被清楚地执行了。
GCOV输出
Summary coverage rate:
lines......: 66.7% (4 of 6 lines)
functions..: 100.0% (2 of 2 functions)
branches...: no data found示例
CMakeLists.txt
set(NAME MinTest)
project (${NAME})
cmake_minimum_required(VERSION 3.5.2)
set(CMAKE_C_FLAGS
"--coverage -O0 -g")
add_executable(
${NAME}
src/main.c
)src/main.c
void function() {
printf("foo");
}
int main(void) {
function();
}runWithCoverageInfo.sh
#!/bin/bash
# $1 = source root
# $2 = build directory
if [ ! -d "coverage" ]; then
echo "Creating coverage directory...";
mkdir coverage;
else
find ./coverage -name *.info -exec rm {} \;;
fi;
echo "Removing previous coverage output files...";
find $2 -name *.gcda -exec rm {} \;;
echo "Analysing baseline coverage data...";
lcov --initial --no-external --capture --base-directory $2 --directory $2 --directory $1 --output-file coverage/coverage_base.info ;
echo "Running tests...";
./MinTest;
returnCode=$?
echo "Generating coverage output";
lcov --capture --no-external --directory $2 --directory $1 --base-directory $2 --output-file coverage/coverage_test.info --quiet ;
lcov -a coverage/coverage_base.info -a coverage/coverage_test.info -output-file coverage/coverage_total.info --quiet;
lcov --summary coverage/coverage_total.info;
genhtml coverage/coverage_total.info --output-directory coverage -quiet;
exit $returnCode;发布于 2016-09-28 05:53:46
曾几何时,LCOV默认提供分支机构覆盖。现在不再是这样了。(至少,我的1.12版本没有。)因此,您必须在lcov和genhtml命令中显式地告诉它,才能生成分支报告。例如:
lcov -d . --zerocounters
lcov -d . --rc lcov_branch_coverage=1 --no-external --capture -o ../reports/myproj.info
cd ../reports
genhtml --branch-coverage myproj.info分支覆盖率分析/报告需要LCOV命令中的--rc lcov_branch_coverage=1和HTML生成命令中的--branch-coverage。
如果希望始终默认为分支覆盖,则可以选择将lcov_branch_coverage=1和genhtml_branch_coverage=1放入.lcovrc资源文件中。有关更多详细信息,请参阅lcovrc(5)手册页,或在此处在线查看:http://ltp.sourceforge.net/coverage/lcov/lcovrc.5.php
https://stackoverflow.com/questions/38415020
复制相似问题