覆盖率工具-jacoco 集成进jenkins

来源:互联网 发布:php 客户端ip 编辑:程序博客网 时间:2024/05/18 01:13

Jacoco是一个开源的覆盖率工具。Jacoco可以嵌入到Ant 、Maven中,并提供了EclEmma Eclipse插件,也可以使用Java Agent技术监控Java程序。很多第三方的工具提供了对Jacoco的集成,如sonar、Jenkins、IDEA. Jacoco包含了多种尺度的覆盖率计数器,包含指令级(Instructions,C0 coverage),分支(Branches,C1 coverage)、圈复杂度(Cyclomatic Complexity)、行(Lines)、方法(non-abstract methods)、类(classes)。

jacoco-maven-plugin

具体的maven配置如下,plugin的版本可以到nexus上搜下,现在nexus上最新的是0.7.2.201409121644:

<!-- jacoco plugin -->            <plugin>                <groupId>org.jacoco</groupId>                <artifactId>jacoco-maven-plugin</artifactId>                <version>0.7.2.201409121644</version>                <executions>                    <execution>                        <!--                        在maven的initialize阶段,将Jacoco的runtime agent作为VM的一个参数                        传给被测程序,用于监控JVM中的调用。                        -->                        <id>default-prepare-agent</id>                        <goals>                            <goal>prepare-agent</goal>                        </goals>                        <configuration>                            <destFile>                                ${project.build.directory}/coverage-reports/jacoco.exec                            </destFile>                            <propertyName>surefireArgLine</propertyName>                        </configuration>                    </execution>                    <!--                    在程序的verify阶段,执行report测试的程序。                    文件的输入为perpare-agent阶段中设置或者默认的jacoco.exec.                    参数 includes和excludes可用来选定report中过滤的类。                    -->                    <execution>                        <id>default-report</id>                        <phase>test</phase>                        <goals>                            <goal>report</goal>                        </goals>                        <configuration>                            <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>                            <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>                        </configuration>                    </execution>                </executions>            </plugin>

jacoco集成进jenkins

1、安装jacoco插件

jenkins->系统管理->管理插件 搜索Jenkins JaCoCo plugin 安装


2、job中配置 post-build-action,生成覆盖率报告


可以配置覆盖率统计的class

Path to exec files:代码覆盖率统计文件位置;

Path to class directories:classes文件位置;

Path to source directories :源码文件位置;

下面是设置覆盖率百分比的提醒阈值。

太阳表示覆盖率高于设置阈值,覆盖率报告里会橙色提醒

乌云表示覆盖率低于设置阈值,覆盖率报告里会橙色提醒


3、在view里面显示覆盖率

本来想在现在的view上直接增加一列,按照官方的方法(https://wiki.jenkins-ci.org/display/JENKINS/Changing+the+Columns+of+a+View)设置无果

所以只能新建一个view


选取job,然后添加一列,如下图,保存即可。


4、查看覆盖率报告

执行job的构建后,会生成覆盖率报告



参考

http://blog.csdn.net/kittyboy0001/article/details/18706067

http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

关于几种覆盖率工具的比较 https://answers.atlassian.com/questions/9880767/clover-vs-cobertura-jacoco-and-other-code-coverage-tools

0 0
原创粉丝点击