How to fail a maven build, if JUnit coverage falls below certain threshold(如果 JUnit 覆盖率低于某个阈值,如何使 Maven 构建失败)
问题描述
我从 sonar rest api 获取单元测试覆盖率指标.
I'm getting the Unit test coverage percentage metric from sonar rest api.
如果构建低于定义的值,我怎么会失败?
How can I fail the build if it falls below a defined value?
推荐答案
JaCoCo
提供了该功能.
使用LINE
和BRANCH
的配置规则COVEREDRATIO
定义JaCoCo插件:
Define JaCoCo plugin using configuration rules COVEREDRATIO
forLINE
and 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>
多种选择
支持的counter
选项有:
- LINE
- 分店
- 说明
- 复杂性
- 方法
- 类
我相信 INSTRUCTION
将允许您进行一般检查(例如,验证整个项目的覆盖率至少为 0.80).
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 示例 - 80% 的总体指令覆盖率
此示例需要 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>
失败消息
如果覆盖范围不符合预期,则会失败并显示以下消息:
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] ------------------------------------------------------------------------
排除
在上面的示例中,我设置了 <exclude>com.xyz.ClassToExclude</exclude>
.我想你会发现你需要添加许多排除项.项目通常包含许多不可测试/测试的类(Spring 配置、Java bean ...).您也可以使用正则表达式.
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.
- http://choudhury.com/blog/2014/02/25/enforcing-minimum-code-coverage
- http://www.eclemma.org/jacoco/trunk/doc/maven.html
- http://www.eclemma.org/jacoco/trunk/doc/check-mojo.html
这篇关于如果 JUnit 覆盖率低于某个阈值,如何使 Maven 构建失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果 JUnit 覆盖率低于某个阈值,如何使 Maven 构建失败
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01