<small id='AvBbO'></small><noframes id='AvBbO'>

  1. <legend id='AvBbO'><style id='AvBbO'><dir id='AvBbO'><q id='AvBbO'></q></dir></style></legend>

      • <bdo id='AvBbO'></bdo><ul id='AvBbO'></ul>

    1. <tfoot id='AvBbO'></tfoot>
      <i id='AvBbO'><tr id='AvBbO'><dt id='AvBbO'><q id='AvBbO'><span id='AvBbO'><b id='AvBbO'><form id='AvBbO'><ins id='AvBbO'></ins><ul id='AvBbO'></ul><sub id='AvBbO'></sub></form><legend id='AvBbO'></legend><bdo id='AvBbO'><pre id='AvBbO'><center id='AvBbO'></center></pre></bdo></b><th id='AvBbO'></th></span></q></dt></tr></i><div id='AvBbO'><tfoot id='AvBbO'></tfoot><dl id='AvBbO'><fieldset id='AvBbO'></fieldset></dl></div>

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

      时间:2024-05-09
      1. <i id='pvwfR'><tr id='pvwfR'><dt id='pvwfR'><q id='pvwfR'><span id='pvwfR'><b id='pvwfR'><form id='pvwfR'><ins id='pvwfR'></ins><ul id='pvwfR'></ul><sub id='pvwfR'></sub></form><legend id='pvwfR'></legend><bdo id='pvwfR'><pre id='pvwfR'><center id='pvwfR'></center></pre></bdo></b><th id='pvwfR'></th></span></q></dt></tr></i><div id='pvwfR'><tfoot id='pvwfR'></tfoot><dl id='pvwfR'><fieldset id='pvwfR'></fieldset></dl></div>

        • <bdo id='pvwfR'></bdo><ul id='pvwfR'></ul>
                  <tbody id='pvwfR'></tbody>

              • <small id='pvwfR'></small><noframes id='pvwfR'>

                <legend id='pvwfR'><style id='pvwfR'><dir id='pvwfR'><q id='pvwfR'></q></dir></style></legend>
                <tfoot id='pvwfR'></tfoot>
                本文介绍了如果 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 提供了该功能.

                使用LINEBRANCH的配置规则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 构建失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:声纳违规:“方法可能无法在异常时关闭流" 下一篇:jacoco 分支覆盖和声纳条件覆盖有什么区别?

                相关文章

                    <legend id='g7nT5'><style id='g7nT5'><dir id='g7nT5'><q id='g7nT5'></q></dir></style></legend>
                      <bdo id='g7nT5'></bdo><ul id='g7nT5'></ul>

                    <small id='g7nT5'></small><noframes id='g7nT5'>

                    <i id='g7nT5'><tr id='g7nT5'><dt id='g7nT5'><q id='g7nT5'><span id='g7nT5'><b id='g7nT5'><form id='g7nT5'><ins id='g7nT5'></ins><ul id='g7nT5'></ul><sub id='g7nT5'></sub></form><legend id='g7nT5'></legend><bdo id='g7nT5'><pre id='g7nT5'><center id='g7nT5'></center></pre></bdo></b><th id='g7nT5'></th></span></q></dt></tr></i><div id='g7nT5'><tfoot id='g7nT5'></tfoot><dl id='g7nT5'><fieldset id='g7nT5'></fieldset></dl></div>

                    <tfoot id='g7nT5'></tfoot>