【问题标题】:Different coverage ratio values in Jacoco check vs jacoco reportJacoco 检查与 jacoco 报告中不同的覆盖率值
【发布时间】:2020-01-14 03:54:13
【问题描述】:

我在我的项目中设置了 Jacoco 来执行 a) 检查代码覆盖率和 b) 生成覆盖率报告。生成的 HTML 报告显示了正确的覆盖率值,但 Jacoco 检查在构建过程结束时生成的警告消息显示了不同的错误值。我想知道我在这里做错了什么。

例如,我可以在生成的 HTML 报告中看到一个 util 类:

但是对于同一个类,我在构建完成时看到了这个:

这是我的 pom.xml 文件中的 jacoco-report 配置:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
        <execution>
            <id>jacoco.check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <phase>${jacoco.check.phase}</phase>
            <configuration>
                <skip>${jacoco.skip}</skip>
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>CLASS</element>
                        <excludes>
                            <exclude>com/xxxxxx/**/config/**/*</exclude>
                            <exclude>com/xxxxxx/**/filter/**/*</exclude>
                            <exclude>com/xxxxxx/xxxxxx/errors/*</exclude>
                            <exclude>com/xxxxxx/xxxxxx/security/*</exclude>
                            <exclude>com/xxxxxx/xxxxxx/xxxxxxOnlineWebApplication.class</exclude>
                        </excludes>
                        <limits>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.90</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
        <execution>
            <id>prepare-unit-tests</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>

        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <propertyName>itCoverageAgent</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我的 jacoco-check 配置如下:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
        <execution>
            <id>agent-for-ut</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.reportPaths}</destFile>
            </configuration>
        </execution>
        <!-- Integration-tests executed with failsafe-plugin -->
        <execution>
            <id>agent-for-it</id>
            <phase>package</phase>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.itReportPath}</destFile>
            </configuration>
        </execution>
    </executions>
    <configuration>
            <excludes>
                <exclude>com/xxxxxx/**/config/**/*</exclude>
                <exclude>com/xxxxxx/**/filter/**/*</exclude>
                <exclude>com/xxxxxx/xxxxxx/errors/*</exclude>
                <exclude>com/xxxxxx/xxxxxx/security/*</exclude>
                <exclude>com/xxxxxx/xxxxxx/xxxxxxOnlineWebApplication.class</exclude>
            </excludes>
    </configuration>
</plugin>

【问题讨论】:

  • 你确定规则能找到Jacoco的数据吗?我看到您已更改 dest 文件位置。规则有一个 dataFile 参数,默认情况下是 /jacoco.exec (有关更多信息,请参阅eclemma.org/jacoco/trunk/doc/check-mojo.html)。
  • 那是他妈的!你是传奇@JamesWilson!非常感谢!
  • 谢谢,我已将我的评论添加为其他任何遇到此问题的人的答案。

标签: unit-testing testing jacoco test-coverage


【解决方案1】:

您似乎正在更改存储结果的位置:

<destFile>${sonar.jacoco.itReportPath}</destFile>

您还需要使用规则配置中的&lt;dataFile&gt; 项告诉检查插件在哪里可以找到数据文件。

请参阅jacoco:check docs 了解更多信息。

【讨论】:

  • 就是这样!很棒的发现。谢谢!
猜你喜欢
  • 2018-09-09
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
相关资源
最近更新 更多