如果 JUnit 覆盖率低于特定阈值,如何使 Maven 构建失败
How to fail a maven build, if JUnit coverage falls below certain threshold
我正在从声纳休息处获取单元测试覆盖率指标 api。
如果构建低于定义值,我如何才能使构建失败?
JaCoCo
提供该功能。
具有配置规则的 JaCoCo
使用配置规则定义 JaCoCo 插件 COVEREDRATIO
for LINE
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
选项是:
- 线
- 分支机构
- 说明
- 复杂性
- 方法
- CLASS
我相信 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>
失败消息
如果覆盖范围不符合预期,则会失败并显示以下消息:
[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>
。我认为您会发现需要添加许多排除项。项目通常包含许多 类 而不是 testable/tested(Spring 配置,Java bean...)。您也可以使用正则表达式。
来源:
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>
如果您使用的是 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>
此代码确保整体覆盖率达到 17%。
请使用
验证这一点
mvn clean verify
命令。
我正在从声纳休息处获取单元测试覆盖率指标 api。
如果构建低于定义值,我如何才能使构建失败?
JaCoCo
提供该功能。
具有配置规则的 JaCoCo
使用配置规则定义 JaCoCo 插件 COVEREDRATIO
for LINE
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
选项是:
- 线
- 分支机构
- 说明
- 复杂性
- 方法
- CLASS
我相信 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>
失败消息
如果覆盖范围不符合预期,则会失败并显示以下消息:
[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>
。我认为您会发现需要添加许多排除项。项目通常包含许多 类 而不是 testable/tested(Spring 配置,Java bean...)。您也可以使用正则表达式。
来源:
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>
如果您使用的是 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>
此代码确保整体覆盖率达到 17%。
请使用
验证这一点mvn clean verify
命令。