如果JUnit覆盖率低于某个阈值,如何使maven构建失败

时间:2017-01-27 10:25:21

标签: java maven jenkins sonarqube code-coverage

我从声纳rest api获得了单元测试覆盖百分比指标。

如果构建低于定义值,如何使构建失败?

3 个答案:

答案 0 :(得分:17)

JaCoCo提供该功能。

JaCoCo与配置规则

使用COVEREDRATIOLINE的配置规则BRANCH定义JaCoCo插件:

<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
  • BRANCH
  • INSTRUCTION
  • 复杂性
  • 方法
  • CLASS

我相信INSTRUCTION会允许您进行一般检查(例如,验证整个项目的覆盖率至少为0.80)。

Example with INSTRUCTION - overall instruction coverage of 80%

  

此示例要求整体指令覆盖率为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>

失败消息

如果覆盖范围不符合预期,则会失败并显示以下消息:

[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 ......)。您也可以使用正则表达式。

来源:

答案 1 :(得分:0)

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>

答案 2 :(得分:0)

如果您使用的是0.8.2或类似版本的jacoco-maven-plugin,请确保已定义数据文件,并对插件使用以下配置。

+--------+-------------+-----------+----------+
| UserID | RechageDate | Avg_Voice | Avg_Data |
+--------+-------------+-----------+----------+
|      1 | 4/8/2018    |      8.33 |    11.66 |
|      1 | 4/10/2018   |        10 |       20 |
|      2 | 4/4/2018    |      27.5 |       25 |
+--------+-------------+-----------+----------+

此代码可确保整体覆盖率达到17%。

请使用

进行验证
  

mvn干净验证

命令。