实践项目:趣头条-实惠喵后端
单元/功能 测试增量覆盖率统计
1.通过测试工具获取到全量覆盖率统计信息,输出到c.out(使用方法见 Golang 全量覆盖率统计),拿单元测试举例:
go test -coverprofile=c.out
c.out内容格式如下:

image.png
2.使用工具gocov和gocov-xml将覆盖率输出转换成xml格式的报告:
gocov convert c.out | gocov-xml > coverage.xml
gocov 安装/说明:https://github.com/axw/gocov/
gocov-xml 安装/说明:https://github.com/AlekSi/gocov-xml
3.使用工具diff-cover获取增量覆盖率:
diff-cover coverage.xml --compare-branch=HEAD^ --html-report report.html --fail-under=80
diff-cover 安装/说明:https://github.com/Bachmann1234/diff_cover
遇到的问题总结
- 运行
diff-cover一直返回:
No lines with coverage information in this diff.
经排查因为之前在将覆盖率统计信息转换成xml格式时,用的gocover-cobertura工具,该工具转换后的格式 diff-cover工具可能未做兼容,导致一直返回如上结果,按照如上步骤2解决了该问题