【C++】统计代码覆盖率(三)

报告集成到jenkins才是最终目的,因此又进行了部分资料查找,得到html和xml报告集成jenkins的配置如下:

一 集成html报告

这种方式集成在你已经用gcov+lcov生成了html报告的基础上,在此只需要将它放到jenkins中,步骤如下:

1 打开jenkins->进入工程->配置->增加构建后操作步骤->Publish HTML reports【C++】统计代码覆盖率(三) 【C++】统计代码覆盖率(三)

2 生成html报告:配置->增加构建步骤->Execute shell

  1. ```
    cd your_path
    sh exittest.sh(上一节中exit程序的脚本)
    cd your_gcda_gcno_path
    lcov -c -o coverage.info -b . -d .
    genhtml coverage.info -o coverage
    cp coverage_result /data/report/coverage #注意该路径与html报告的读取路径一致


<span style="font-size: 15px">**3 运行完毕,即可在jenkins页面左侧和中上侧看到THML Report 点开即可查看详情,如下**</span>![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704113131030-1615099911.png)



<span style="font-size: 18px">**<span style="color: rgba(255, 0, 255, 1)">二 集成Cobertura的xml报告</span>**</span>

集成到Cobertura与集成到html类似,都需要配置构建后步骤和Execute shell,如下

<span style="font-size: 16px; color: rgba(0, 0, 0, 1)">**1 jenkins的Cobertura插件**</span>

**1) jenkins安装插件Cobertura:**jenkins页面->系统管理->管理插件,可在该页面搜索安装插件Cobertura

![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704113528592-1159990803.png)

**2)在工程的配置中,增加构建后操作步骤**->Publish Cobertura Coverage Report

![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704113958186-336498764.png)

**3) 配置Cobertura**

![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704114135499-1558761163.png)

<span style="font-size: 16px">**2 配置Execute shell,得到xml文件**</span>

**1)在jenkins的工程配置中增加构建步骤->Execute shell**

sudo sh /data/ad_server/exittest.sh
cd your_code_path
sudo gcovr --xml-pretty -r . --output /data/AdServerTest/coverage.xml


**2)在jenkins构建后即可查看结果**

![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704114505264-582509626.png)



<span style="font-size: 18px; color: rgba(255, 0, 255, 1)">**三 问题**</span>

**1 jenkins配置后生成xml报告失败**,报错如下:

![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704115918358-1411667680.png)

[Cobertura] No coverage results were found using the pattern '/data/cppcoverage/xml/coverage.xml' relative to '/data/AdServerTest'. Did you enter a pattern relative to the correct directory? Did you generate the XML report(s) for Cobertura?


原因:jenkins路径未关联,具体我也还没搞清楚,貌似Cobertura只能使用相对路径。<span style="text-decoration: underline">***将生成的xml报告更改为目录/data/AdServerTest下的目录即可***</span>路径后即可。



2 在查看详细报告时,无法查看具体代码行数覆盖,提示Source code is unavailable,如下:

![](https://www.ccppcoding.com/wp-content/uploads/742895-20160704120135530-615251599.png)

原因:怀疑是jenkins路径的缘故,存放xml文件的路径下并没有源码,具体我还没解决,后续补充

解决:我加载的coverage.xml文件目录为/data/AdServer/reports/coverag.xml目录,源码目录为/data/jenkins/workspace/sourceCode,该文件起始部分如下:

. //表示会从当前目录读取源码-source code


因此不能显示源码,更改生成xml文件时表示源码路径的'.'为绝对路径即可:sudo gcovr --xml-pretty -r /data/jenkins/workspace/sourceCode/ --output /data/AdServerTest/coverage.xml

/data/jenkins/workspace/sourceCode/

```

得到结果图如下,覆盖部分为绿色,未覆盖部分为红色:

【C++】统计代码覆盖率(三)

原文链接: https://www.cnblogs.com/zhaoxd07/p/5631644.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/236335

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午5:00
下一篇 2023年2月13日 下午5:00

相关推荐