Gcovr 入门指导
在上一节Gtest的学习中,我们对Money Demo通过gtest进行了测试,那么我们的 测试覆盖了代码中所有的情况么?测试的完全么?也许我们不想花费太多的时间 纠结在测试上,所以下面推荐一款代码覆盖率的工具(gcovr)以供大家检查自己的 代码覆盖情况。强调一下,我们最好是在linux下学习gcovr的使用。
那么什么是Gcovr呢,我们看一下Gcovr的guide.pdf中是如何描述的:
“Gcovr provides a utility for managing the use of the GNU gcov utility and generating summarized code coverage results. This command is inspired by the Python coverage.py package, which provides a similar utility in Python. The gcovr command produces either compact human-readable summary reports, machine readable XML reports (in Cobertura format) or simple HTML reports. Thus, gcovr can be viewed as a command-line alternative to the lcov utility, which runs gcov and generates an HTML-formatted report.”
Table of Contents
• 1 Gcovr的下载:
• 2 Gcovr的使用:
o 2.1 Makefile
o 2.2 Gcovr的具体命令参数的使用
2.2.1 -r 参数
2.2.2 –branches
2.2.3 XML输出文件
2.2.4 HTML输出
2.2.5 –html-details
2.2.6 Gcovr的其他命令
• 3 Gcovr注意事项
• 4 作业
• 5 参考文献
1 Gcovr的下载:
http://gcovr.com/
上面给出的连接是gcovr的home page。在home page中我们能下载到有关gcovr 的PDF,HTML等一些指导书籍。
大家可以从下面这个链接处下载到Gcvor,下载最新的3.2即可。
https://github.com/gcovr/gcovr/releases
2 Gcovr的使用:
当我们把下载的gcovr-3.2.zip(或者是gcovr.tar.gz)解压缩,比如我的解压缩 的位置是:/home/yulixiang/work/GtestAndGcovr/gcovr-3.2。
我的Money的源码位置是: /home/yulixiang/work/GtestAndGcovr/SourceCode
那么Gcovr和Money源码SourceCode是同级目录。
下面我们还是根据Money例子来讲述如何使用Gcovr。首先我们要知道那怎么才 能在我们的项目中加入Gcovr。
2.1 Makefile
在Money例子的Makefile编译选项中加入编译选项: -fprofile-arcs -ftest-coverage -fPIC -O0。 在链接选项中加入-lgcov选项即可。就是这么 简单!
// 编译选项
CXXFLAGS +=-c -g -Wall -Wextra -I$(GTEST_DIR)/include -fprofile-arcs -ftest-coverage -fPIC -O0
//链接选项
LDFLAGS += -L$(GTEST_DIR)/lib -lgtest -lpthread -lgcov
在加入这些编译和链接选项之后,整个Makefile的代码如下:
GTEST_DIR=your_gtest_directory
SRC_DIR=your_Money_SouceCode_directory
LDFLAGS += -L$(GTEST_DIR)/lib -lgtest -lpthread -lgcov
CXXFLAGS +=-c -g -Wall -Wextra -I$(GTEST_DIR)/include -fprofile-arcs -ftest-coverage -fPIC -O0
TARGETS = money_unittest
OBJS = money.o gtestMoney.o
CC=g++
.PHONY: clean all test
all: $(TARGETS)
$(TARGETS) : $(OBJS)
$(CC) $^ -o $@ $(LDFLAGS)
money.o : $(SRC_DIR)/Money.cpp $(SRC_DIR)/Money.h
$(CC) $(CXXFLAGS) $< -o $@
gtestMoney.o : $(SRC_DIR)/MoneyTest.cpp $(SRC_DIR)/Money.h
$(CC) $(CXXFLAGS) $< -o $@
clean:
rm -f $(TARGETS) $(OBJS)
rm *.gcno
rm *.gcda
rm *.xml
test: ( T A R G E T S ) . / (TARGETS) ./ (TARGETS)./(TARGETS)
在更新Makefile之后我们编译(make)就会有.gcno文件产生,我们执行 money_unittest.exe就会有.gcda文件产生。这两种文件是Gcovr分析的时候 会用到的二进制文件。那现在Gcovr分析所需要的文件已经有了怎么使用 Gcovr来分析Money的代码覆盖率呢?
2.2 Gcovr的具体命令参数的使用
2.2.1 -r 参数
因为上文我把gcovr-3.2文件夹放在与我们的SourceCode是同级目录,所以我 们可以在SourceCode中直接执行下面的命令:
// 执行gcovr
~/work/GtestAndGcovr/SourceCode> python …/gcovr-3.2/scripts/gcovr -r .
就会产生如下的结果
‘-r’ 操作只是产生一个简单的报告。从输出的结果我们可以看到Money.cpp文 件共有13行,但之执行了7行文件。覆盖率仅仅是53% 。那么我们很想知道具体都 有哪些行没有覆盖,这个时候用下列这些命令了。
2.2.2 –branches
–branches命令能够总结出每个项目文件有多少个branches,测试用例中覆盖了 多少个branches。
2.2.3 XML输出文件
Gcovr在默认的条件下是生成一个纯文本的表格。Gcovr能够生成一个XML的输 出文件,使用–xml 和 –xml-pretty
~/work/GtestAndGcovr/SourceCode> python …/gcovr-3.2/scripts/gcovr -r . --xml --xml-pretty
由于输出很多,所以我添加到附件XMLOutput.xml文件中,下面是输出的一部分:
–html命令只是产生一个总结所有的文件信息的单一网页。
2.2.5 –html-details
–html-details命令是为每一个项目文件产生一个单独的网页。每一个单独 的网页都包含了该文件详细的代码覆盖情况。
• 注意: 在使用–html-details必须同时使用-o命令。
~/work/GtestAndGcovr/SourceCode> python …/gcovr-3.2/scripts/gcovr -r . --html --html-details -o result.html
执行这个命令后我们可以看到会有:result.html, result.Money.cpp.html, result.Money.h.html和result.MoneyTest.cpp.html四个HTML文件产生。当我们 双击result.html。
可以看到和–html命令一样的页面,但是现在我们能够点击.cpp,跳转到 Money.cpp文件的代码覆盖情况的页面。一般情况下,我们也是最关心.cpp文件的 覆盖情况。
通过这个包含了详细信息的HTML文件,我们可以直观的看到,我们的测试代码没 有覆盖operator==, opreator!= 这两个功能接口,也没有覆盖operator +=中的 一个分支。根据这些详细的信息有助于我们修改测试。
2.2.6 Gcovr的其他命令
通过 gcovr –help 就可以显示出gcovr的命令摘要。
~/work/GtestAndGcovr/SourceCode> python …/gcovr-3.2/scripts/gcovr --help
Usage: gcovr [options]
A utility to run gcov and generate a simple report that summarizes the
coverage
Options:
-h, --help show this help message and exit
–version Print the version number, then exit
-v, --verbose Print progress messages
–object-directory=OBJDIR
Specify the directory that contains the gcov data
files. gcovr must be able to identify the path
between the *.gcda files and the directory where gcc
was originally run. Normally, gcovr can guess
correctly. This option overrides gcovr’s normal path
detection and can specify either the path from gcc to
the gcda file (i.e. what was passed to gcc’s ‘-o’
option), or the path from the gcda file to gcc’s
original working directory.
-o OUTPUT, --output=OUTPUT
Print output to this filename
-k, --keep Keep the temporary *.gcov files generated by gcov. By
default, these are deleted.
-d, --delete Delete the coverage files after they are processed.
These are generated by the users’s program, and by
default gcovr does not remove these files.
-f FILTER, --filter=FILTER
Keep only the data files that match this regular
expression
-e EXCLUDE, --exclude=EXCLUDE
Exclude data files that match this regular expression
–gcov-filter=GCOV_FILTER
Keep only gcov data files that match this regular
expression
–gcov-exclude=GCOV_EXCLUDE
Exclude gcov data files that match this regular
expression
-r ROOT, --root=ROOT Defines the root directory for source files. This is
also used to filter the files, and to standardize the
output.
-x, --xml Generate XML instead of the normal tabular output.
–xml-pretty Generate pretty XML instead of the normal dense
format.
–html Generate HTML instead of the normal tabular output.
–html-details Generate HTML output for source file coverage.
–html-absolute-paths
Set the paths in the HTML report to be absolute
instead of relative
-b, --branches Tabulate the branch coverage instead of the line
coverage.
-u, --sort-uncovered Sort entries by increasing number of uncovered lines.
-p, --sort-percentage
Sort entries by decreasing percentage of covered
lines.
–gcov-executable=GCOV_CMD
Defines the name/path to the gcov executable [defaults
to the GCOV environment variable, if present; else
‘gcov’].
–exclude-unreachable-branches
Exclude from coverage branches which are marked to be
excluded by LCOV/GCOV markers or are determined to be
from lines containing only compiler-generated “dead”
code.
-g, --use-gcov-files Use preprocessed gcov files for analysis.
-s, --print-summary Prints a small report to stdout with line & branch
percentage coverage
3 Gcovr注意事项
我们使用gcovr检查代码的覆盖率,要求是覆盖率越大越好,包括代码branch的 覆盖和line覆盖。
4 作业
通过这一课的学习,要求大家对自己的上一次作业DemoContainer,进行代码覆 盖的检查,并且根据提供的信息,补全我们的代码测试。例如我们的作业可以 达到下面的覆盖结果(gcovr命令用的是gcovr -r SourceCode/ –html –html-details –branch -o result.html)。我们最关心的是.cpp( DemoContainer.cpp)文件的覆盖率,从下面的截图我们可以看出 DemoContainer.cpp文件的line和branch覆盖均为100%。这是最理想的结果。
5 参考文献
http://gcovr.com/
Author: 于丽香 <yu-lx@neusoft.com >
Date: 2018-11-03 15:00:52 CST
HTML generated by org-mode 6.33x in emacs 23
Gtest 测试指导 入门基础(A)
Table of Contents
• 1 Gtest的基本使用,包括下载,安装,编译。
o 1.1 下载
o 1.2 编译
1.2.1 Gtest静态库的编译
1.2.2 Gtest在VS中的编译
• 2 在项目中配置Gtest
o 2.1 Gtest在非VS环境下的配置
o 2.2 Gtest在VS环境下的配置
• 3 Gtest的使用
o 3.1 Makefile
o 3.2 构建代码
o 3.3 Gtest断言的使用
o 3.4 Gtest的异常检查
o 3.5 Gtest的事件机制
o 3.6 Gtest的参数化
o 3.7 Gtest的死亡测试
3.7.1 *_DEATH(statement, regex)
3.7.2 *_EXIT(statement, predicate, regex)
3.7.3 死亡测试运行方式
3.7.4 死亡测试的注意事项
o 3.8 Gtest的运行参数
• 4 作业
o 4.1 编写一个DemoContainer类,按以下接口要求实现:
• 5 参考文献
1 Gtest的基本使用,包括下载,安装,编译。
1.1 下载
直接在google中搜索gtest,第一个就是。也可以从下面地址下载gtest。
https://code.google.com/p/googletest/downloads/
1.2 编译
1.2.1 Gtest静态库的编译
在下载解压后,假设你把gtest源码放在/usr/src/gtest
GTEST_DIR=/usr/src/gtest
设置完GTEST_DIR之后,执行下列的命令
g++ -I G T E S T D I R / i n c l u d e