使用lcov生成覆盖率报告
- 1- 需要准备的东西
- 1.1 工具lcov
- 1.2 需要用到中间脚本 gcno gcda
- 1.3 源文件
- 2- 生成覆盖率报告
- 2.1 step1: 编译阶段
- 2.2 step2: 数据收集与提取阶段
- 2.3 step3: 报告形成阶段
- 2.4 step4: lcov生成覆盖率报告结果info文件
- 2.5 step5: genhtml 命令生成网页版的覆盖率报告文件夹
- 3- 覆盖率报告结果
- 3.1 总结果页面
- 3.2 单独源文件页面
- 3.3 代码执行覆盖率页面
1- 需要准备的东西
1.1 工具lcov
下载解压安装
我这里用的是 lcov-1.14.tar.gz
解压
tar -xzvf lcov-1.14.tar.gz
安装
cd lcov-1.14
make install
1.2 需要用到中间脚本 gcno gcda
gcno文件 包含相应源码的行号信息,程序流图信息
gcda文件 收集数据
1.3 源文件
要执行的源文件 SimpleCoverage.c
ll
显示
-rw-rw-r-- 1 tester tester 353 Jun 14 23:25 SimpleCoverage.c
2- 生成覆盖率报告
2.1 step1: 编译阶段
当前工作路径 ~/TestCoverage2023
生成可执行文件和gcno文件
生成可执行文件 gcc SimpleCoverage.c -o SimpleCoverage
生成gcno文件 -fprofile-arcs -ftest-coverage
执行命令
gcc -fprofile-arcs -ftest-coverage SimpleCoverage.c -o SimpleCoverage
生成了gcno文件
ll
total 32
-rwxrwxr-x 1 tester tester 24104 Jun 14 23:27 SimpleCoverage
-rw-rw-r-- 1 tester tester 353 Jun 14 23:25 SimpleCoverage.c
-rw-rw-r-- 1 tester tester 996 Jun 14 23:27 SimpleCoverage.gcno
2.2 step2: 数据收集与提取阶段
执行可执行文件即可 生成 gcda文件
./SimpleCoverage
显示源代码SimpleCoverage.c 的执行结果
main()---
show()-
i=0
i=1
i=2
no
生成了gcda文件
ll
total 36
-rwxrwxr-x 1 tester tester 24104 Jun 14 23:27 SimpleCoverage
-rw-rw-r-- 1 tester tester 353 Jun 14 23:25 SimpleCoverage.c
-rw-rw-r-- 1 tester tester 252 Jun 14 23:33 SimpleCoverage.gcda
-rw-rw-r-- 1 tester tester 996 Jun 14 23:27 SimpleCoverage.gcno
2.3 step3: 报告形成阶段
依赖 gcno和gcda 生成gcov 文件
执行命令
gcov SimpleCoverage.c
显示
File 'SimpleCoverage.c'
Lines executed:100.00% of 11
Creating 'SimpleCoverage.c.gcov'
生成了gcov文件
ll
total 40
-rwxrwxr-x 1 tester tester 24104 Jun 14 23:27 SimpleCoverage
-rw-rw-r-- 1 tester tester 353 Jun 14 23:25 SimpleCoverage.c
-rw-rw-r-- 1 tester tester 878 Jun 14 23:35 SimpleCoverage.c.gcov
-rw-rw-r-- 1 tester tester 252 Jun 14 23:33 SimpleCoverage.gcda
-rw-rw-r-- 1 tester tester 996 Jun 14 23:27 SimpleCoverage.gcno
2.4 step4: lcov生成覆盖率报告结果info文件
执行命令
lcov -c -d . -o SimpleCoverage.info
Capturing coverage data from .
Found gcov version: 4.8.5
Scanning . for .gcda files ...
Found 1 data files in .
Processing SimpleCoverage.gcda
Finished .info-file creation
生成了info文件
ll
total 40
-rwxrwxr-x 1 tester tester 24104 Jun 14 23:27 SimpleCoverage
-rw-rw-r-- 1 tester tester 353 Jun 14 23:25 SimpleCoverage.c
-rw-rw-r-- 1 tester tester 252 Jun 14 23:33 SimpleCoverage.gcda
-rw-rw-r-- 1 tester tester 996 Jun 14 23:27 SimpleCoverage.gcno
-rw-rw-r-- 1 tester tester 239 Jun 14 23:39 SimpleCoverage.info
2.5 step5: genhtml 命令生成网页版的覆盖率报告文件夹
这里报告文件夹起名为 CovReport
执行命令
genhtml -o CovReport SimpleCoverage.info
Reading data file SimpleCoverage.info
Found 1 entries.
Found common filename prefix "/home/xxx"
Writing .css and .png files.
Generating output.
Processing file TestCoverage2023/SimpleCoverage.c
Writing directory view page.
Overall coverage rate:
lines......: 100.0% (11 of 11 lines)
functions..: 100.0% (2 of 2 functions)
生成了覆盖率报告文件夹 CovReport
ll
total 40
drwxrwxr-x 3 tester tester 217 Jun 14 23:41 CovReport
-rwxrwxr-x 1 tester tester 24104 Jun 14 23:27 SimpleCoverage
-rw-rw-r-- 1 tester tester 353 Jun 14 23:25 SimpleCoverage.c
-rw-rw-r-- 1 tester tester 252 Jun 14 23:33 SimpleCoverage.gcda
-rw-rw-r-- 1 tester tester 996 Jun 14 23:27 SimpleCoverage.gcno
-rw-rw-r-- 1 tester tester 239 Jun 14 23:39 SimpleCoverage.info
ll ~/TestCoverage2023/CovReport/
total 48
-rw-rw-r-- 1 tester tester 141 Jun 14 23:41 amber.png
-rw-rw-r-- 1 tester tester 141 Jun 14 23:41 emerald.png
-rw-rw-r-- 1 tester tester 9893 Jun 14 23:41 gcov.css
-rw-rw-r-- 1 tester tester 167 Jun 14 23:41 glass.png
-rw-rw-r-- 1 tester tester 3709 Jun 14 23:41 index.html
-rw-rw-r-- 1 tester tester 3702 Jun 14 23:41 index-sort-f.html
-rw-rw-r-- 1 tester tester 3702 Jun 14 23:41 index-sort-l.html
-rw-rw-r-- 1 tester tester 141 Jun 14 23:41 ruby.png
-rw-rw-r-- 1 tester tester 141 Jun 14 23:41 snow.png
drwxrwxr-x 2 tester tester 183 Jun 14 23:41 TestCoverage2023
-rw-rw-r-- 1 tester tester 117 Jun 14 23:41 updown.png
ll ~/TestCoverage2023/CovReport/TestCoverage2023/
total 28
-rw-rw-r-- 1 tester tester 3800 Jun 14 23:41 index.html
-rw-rw-r-- 1 tester tester 3793 Jun 14 23:41 index-sort-f.html
-rw-rw-r-- 1 tester tester 3793 Jun 14 23:41 index-sort-l.html
-rw-rw-r-- 1 tester tester 3434 Jun 14 23:41 SimpleCoverage.c.func.html
-rw-rw-r-- 1 tester tester 3427 Jun 14 23:41 SimpleCoverage.c.func-sort-c.html
-rw-rw-r-- 1 tester tester 5008 Jun 14 23:41 SimpleCoverage.c.gcov.html
wizard07.adv.advantest.com$