java 如果 JUnit 覆盖率低于特定阈值,如何使 Maven 构建失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41891739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fail a maven build, if JUnit coverage falls below certain threshold
提问by rathna
I'm getting the Unit test coverage percentage metric from sonar rest api.
我正在从声纳休息 api 获取单元测试覆盖率指标。
How can I fail the build if it falls below a defined value?
如果构建低于定义的值,我该如何失败?
回答by alexbt
JaCoCo
offers that feature.
JaCoCo
提供该功能。
JaCoCo with Configuration rules
带有配置规则的JaCoCo
Define JaCoCo plugin using configuration rules COVEREDRATIO
forLINE
and BRANCH
:
定义JaCoCo插件使用配置规则COVEREDRATIO
的LINE
和BRANCH
:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
<excludes>
<exclude>com.xyz.ClassToExclude</exclude>
</excludes>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
Various options
多种选择
The supported counter
options are:
支持的counter
选项是:
- LINE
- BRANCH
- INSTRUCTION
- COMPLEXITY
- METHOD
- CLASS
- 线
- 分支
- 操作说明
- 复杂
- 方法
- 班级
I believe INSTRUCTION
would allow you to make a general check (verify that the whole project has at least 0.80 of coverage for instance).
我相信INSTRUCTION
可以让您进行一般检查(例如,验证整个项目的覆盖率至少为 0.80)。
Example with INSTRUCTION - overall instruction coverage of 80%
This example requires an overall instruction coverage of 80% and no class must be missed:
<rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>INSTRUCTION</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <limit implementation="org.jacoco.report.check.Limit"> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules>
这个例子需要 80% 的整体指令覆盖率并且不能错过任何课程:
<rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>INSTRUCTION</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <limit implementation="org.jacoco.report.check.Limit"> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules>
Message on failure
失败消息
If the coverage isn't as expected, it fails with the following message:
如果覆盖范围不符合预期,则会失败并显示以下消息:
[WARNING] Rule violated for class com.sampleapp.SpringConfiguration: lines covered ratio is 0.00, but expected minimum is 0.80
[WARNING] Rule violated for class com.sampleapp.Launcher: lines covered ratio is 0.33, but expected minimum is 0.80
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
Exclusions
排除
In the example above, I set <exclude>com.xyz.ClassToExclude</exclude>
. I think you'll find you need to add many exclusions. Projects usually contain many classes which aren't testable/tested (Spring Configuration, Java beans...). You may be able to use regular expression too.
在上面的例子中,我设置了<exclude>com.xyz.ClassToExclude</exclude>
. 我想你会发现你需要添加许多排除项。项目通常包含许多不可测试/测试的类(Spring 配置、Java bean...)。您也可以使用正则表达式。
sources:
来源:
回答by user3069716
Specify the datFile location if it is not under default folder.
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile><path-to-jacoc-ut.exec></dataFile>
<rules><rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
<excludes>
<exclude>com.xyz.ClassToExclude</exclude>
</excludes>
</rule></rules></configuration>
</execution>
回答by Suraj Muraleedharan
If you are using 0.8.2 or similar versions of jacoco-maven-plugin, please make sure data file is defined, and use the following configuration for the plugin.
如果您使用的是 0.8.2 或类似版本的 jacoco-maven-plugin,请确保已定义数据文件,并为插件使用以下配置。
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.17</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
This code ensures overall coverage of 17%.
此代码可确保 17% 的整体覆盖率。
Please verify this using
请使用
mvn clean verify
mvn 清洁验证
command.
命令。