【问题标题】:Jacoco Showing Wrong Coverage check ResultJacoco 显示错误的覆盖率检查结果
【发布时间】:2019-09-14 22:49:16
【问题描述】:

我已经通过 maven 在我的项目中配置了我的 Jacoco 插件。

这是我的 jacoco 配置

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.3</version>
    <configuration>
      <excludes>
      </exclude>**/some/package/SomeClass*</exclude>
      </excludes>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
      </execution>
      <execution>
        <id>report</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>report</goal>
        </goals>
      </execution>
      <execution>
        <id>jacoco-check</id>
        <goals>
          <goal>check</goal>
        </goals>
        <configuration>
          <rules>
            <rule>
              <element>CLASS</element>
              <excludes>
                <exclude>*Test</exclude>
              </excludes>
              <limits>
                <limit>
                  <counter>LINE</counter>
                  <value>COVEREDRATIO</value>
                  <minimum>80%</minimum>
                </limit>
              </limits>
            </rule>
          </rules>
        </configuration>
      </execution>
    </executions>
  </plugin>

我已经执行了测试,并显示了 94% 的抽象类覆盖率,我使用它的具体实现测试了这个抽象类。

当我通过 maven build 运行时

我收到以下错误

违反了班级规则 my.package.AbstractParser.1: 线覆盖率为 0.00,但预期最小值为 0.80

我尝试在 Test 上使用虚拟实现来测试抽象类,但仍然遇到同样的错误

谁能告诉我我在这里做错了什么。

编辑: 我找到了失败的原因

我写了一个内联地图初始化

return new HashMap<String, String>() {
    {
      put(input, "");
    }
  };

并且该部分的覆盖率显示为 0%。所以我的测试没有涵盖这部分。

但是我累了

 final Map<String, String> defaultMap = new HashMap<>();
  defaultMap.put(input, "");
  return defaultMap;

构建过程甚至没有覆盖新代码。有人可以解释一下为什么内联初始化会发生这种情况吗???

【问题讨论】:

    标签: java unit-testing jacoco-maven-plugin test-coverage


    【解决方案1】:

    你的配置

              <rules>
                <rule>
                  <element>CLASS</element>
                  <excludes>
                    <exclude>*Test</exclude>
                  </excludes>
                  <limits>
                    <limit>
                      <counter>LINE</counter>
                      <value>COVEREDRATIO</value>
                      <minimum>80%</minimum>
                    </limit>
                  </limits>
                </rule>
              </rules>
    

    表示每个类的线路覆盖率应至少为 80%。

    return new HashMap<String, String>() {
        {
          put(input, "");
        }
      };
    

    声明 anonymous class,顺便说一句,JaCoCo 报告中可见的内容 - 请参阅下面屏幕截图中的第一行表格

      final Map<String, String> defaultMap = new HashMap<>();
      defaultMap.put(input, "");
      return defaultMap;
    

    不声明任何类。

    【讨论】:

      【解决方案2】:

      尝试将此添加到您的 gradle 构建中

      android {
          testOptions {
              unitTests {
                  all {
                      jvmArgs '-noverify'
                  }
              }
          }
      }
      

      测试和覆盖率有问题,所以你需要配置jvmArgs设置进行覆盖率检查,它可以在IDE本身中启用,但是在CI/maven/where中运行覆盖率时应该在gradle中配置

      【讨论】:

        猜你喜欢
        • 2017-05-31
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 2014-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多