unit-testing - Jacoco 检查与 jacoco 报告中的不同覆盖率值

标签 unit-testing testing jacoco test-coverage

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

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

jacoco report

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

jacoco check warning

这是我的 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>

最佳答案

看起来您正在更改存储结果的位置:

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

您还需要使用 <dataFile> 告诉检查插件在哪里可以找到数据文件。项目添加到您的规则配置中。

请参阅jacoco:check docs获取更多信息。

关于unit-testing - Jacoco 检查与 jacoco 报告中的不同覆盖率值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57903852/

相关文章:

c# - Assert.AreEquals 调用 object.Equals,无论 IEquatable 实现如何

testing - 如何在 iMacros 脚本中插入注释?

c# - 如何在 C# 中使用 PostMessage 发送回车键?

android - 使用 JaCoCo 和 Gradle 的 espresso 测试代码覆盖率为 0%

unit-testing - 如何使用注入(inject)了其他服务的服务来测试 grails 3 Controller ?

java - 在 java 中使用 mockito 记录器

java - 单元测试,我应该修改方法参数吗

testing - NUnit 有时以 "unable to locate fixture"结尾

java - Arquillian 测试覆盖率

java - Gradle 和插件恶作剧,用于多模块构建