内容简介:代码覆盖率测试反映了测试的广度与深度,量化了测试和开发质量,是十分有必要的,业界目前有针对各种语言的覆盖率测试工具,本文主要介绍测试代码如下:
代码覆盖率测试反映了测试的广度与深度,量化了测试和开发质量,是十分有必要的,业界目前有针对各种语言的覆盖率测试工具,本文主要介绍 C/C++ 相关的覆盖率测试工具 Gcov
介绍
Gcov 是一个测试覆盖程序,是集成在 GCC 中的,随 GCC 一起发布
使用
编译
测试代码如下:
say.c :
#include <stdio.h>
int say(char *what) {
printf("------ %s\n", what);
return 0;
}
main.c
#include <stdio.h>
extern int say(const char *);
int main(int argc, const char *argv[]) {
if (argv[1]) {
say("hello");
} else {
say("bye");
}
return 0;
}
添加 -fprofile-arcs -ftest-coverage 编译参数编译程序,生成可执行程序和 *.gcno 文件:
$ gcc -fprofile-arcs -ftest-coverage -O0 say.c main.c $ ls a.out main.c main.gcno say.c say.gcno
数据收集
运行可执行文件,生成 *.gcda :
$ ./a.out ------ bye $ ls a.out main.c main.gcda main.gcno say.c say.gcda say.gcno
报告生成
可视化数据
xxxxxxxxxxxxxxxxxxxxxxxxxxx
列表形式
- 代码覆盖率
$ gcovr -r . ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Lines Exec Cover Missing ------------------------------------------------------------------------------ main.c 5 4 80% 15 say.c 3 3 100% ------------------------------------------------------------------------------ TOTAL 8 7 87% ------------------------------------------------------------------------------
报告展示程序运行后覆盖了 80% 的代码
- 分支覆盖率
$ gcovr -b -r . ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Branches Taken Cover Missing ------------------------------------------------------------------------------ main.c 2 1 50% 14 say.c 0 0 --% ------------------------------------------------------------------------------ TOTAL 2 1 50% ------------------------------------------------------------------------------
报告展示了在 main.c 中有一个分支没有执行到
XML文件形式
$ gcovr --xml-pretty -r .
<?xml version="1.0" ?>
<!DOCTYPE coverage
SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-04.dtd'>
<coverage branch-rate="0.5" branches-covered="1" branches-valid="2"
complexity="0.0" line-rate="0.875" lines-covered="7" lines-valid="8"
timestamp="1537930892" version="gcovr 3.4">
<sources>
<source>.</source>
</sources>
<packages>
<package branch-rate="0.5" complexity="0.0" line-rate="0.875" name="">
<classes>
<class branch-rate="0.5" complexity="0.0" filename="main.c"
line-rate="0.8" name="main_c">
<methods/>
<lines>
<line branch="false" hits="1" number="12"/>
<line branch="true" condition-coverage="50% (1/2)" hits="1" number="14">
<conditions>
<condition coverage="50%" number="0" type="jump"/>
</conditions>
</line>
<line branch="false" hits="0" number="15"/>
<line branch="false" hits="1" number="17"/>
<line branch="false" hits="1" number="19"/>
</lines>
</class>
<class branch-rate="0.0" complexity="0.0" filename="say.c" line-rate="1.0"
name="say_c">
<methods/>
<lines>
<line branch="false" hits="1" number="10"/>
<line branch="false" hits="1" number="11"/>
<line branch="false" hits="1" number="12"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
HTML文件形式
$ gcovr -r . --html -o xxx.html $ ls a.out main.c main.gcda main.gcno say.c say.gcda say.gcno xxx.html
可以发现添加 --html 参数后,可以生成 html 文件,用浏览器打开,如下图:
还可以添加 --html-details 选项,为每个代码文件单独生成 html
$ gcovr -r . --html --html-details -o xxx.html $ ls a.out main.c main.gcda main.gcno say.c say.gcda say.gcno xxx.html xxx.main.c.html xxx.say.c.html
可以发现多了 xxx.main.c.html 和 xxx.say.c.html ,用浏览器打开 xxx.html ,如下图:
文件名较之前带上了下划线,单击文件名,可以看到具体的代码覆盖情况,如下图:
Reference
About me
- GitHub: AnSwErYWJ
- Blog: http://www.answerywj.com
- Email: yuanweijie1993@gmail.com
- Weibo:@AnSwEr不是答案
- CSDN: AnSwEr不是答案的专栏
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License .
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。
gcovr是一个用 Python 编写的开源软件,大小只有几十KB
*.gcno 文件, 它包含了程序块和行号等信息
gcovr的下载地址是: https://blog.csdn.net/zhouzhaoxiong1227/article/details/50352944?utm_source=copy
代码覆盖率
-fprofile-arcs -ftest-coverage
分支覆盖率
gcovr -r . –branches
展示覆盖率的XML文件
gcovr -r . –xml-pretty
展示覆盖率的HTML文件
gcovr -r . –html -o FindStackDirection.html
添加“–html-details”选项为代码工程中的每个文件生成一个独立的web页
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Visual Thinking
Colin Ware / Morgan Kaufmann / 2008-4-18 / USD 49.95
Increasingly, designers need to present information in ways that aid their audiences thinking process. Fortunately, results from the relatively new science of human visual perception provide valuable ......一起来看看 《Visual Thinking》 这本书的介绍吧!