简体   繁体   English

Jacoco显示错误的承保范围检查结果

[英]Jacoco Showing Wrong Coverage check Result

I have configure My Jacoco plugin In my project via maven. 我已经通过maven在我的项目中配置了Jacoco插件。

Here is my jacoco configuration 这是我的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>

截图

I have executed the Test, and shows 94% coverage for an Abstract Class , I tested this abstract class using it's concrete Implementation. 我已经执行了Test,并显示了抽象类的94%的覆盖率,我使用了它的具体实现对其进行了测试。

When i run by maven build 当我通过maven build运行时

I'm getting following error 我收到以下错误

Rule violated for class my.package.AbstractParser.1: lines covered ratio is 0.00, but expected minimum is 0.80 类my.package.AbstractParser.1违反了规则:行覆盖率是0.00,但预期最小值是0.80

I tried to test abstract class using a dummy implementation on Test still I'm getting the same error 我尝试在Test上使用虚拟实现来测试抽象类,但仍然遇到相同的错误

Can some one tell me what I'm doing wrong here. 有人可以告诉我我在做什么错。

EDIT: I found out the cause for failure 编辑:我找出了失败的原因

I have written an inline map initialization 我已经写了一个内联地图初始化

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

And the coverage was showing 0% against this part . 相对于该部分,覆盖率显示为0%。 so my test was not covering this part. 所以我的测试没有涵盖这部分。

But I tired 可是我累了

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

The build pass without even coverage around new code. 构建过程甚至没有覆盖新代码。 can some one explain me why it happened with inline initialization ??? 有人可以解释一下为什么内联初始化会发生吗?

Your 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>

means that line coverage should be at least 80% for each class . 表示每个类别的线路覆盖率至少应为80%。

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

declares anonymous class , what is BTW visible in JaCoCo report - see first table row on screenshot below 声明匿名类 ,JaCoCo报告中可见什么是BTW-请参见下面的屏幕快照的第一表行

报告

Whereas

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

doesn't declare any class. 不声明任何类。

Try adding this to your gradle build 尝试将其添加到您的gradle构建中

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

There is an issue with tests and coverage, so you need to configure jvmArgs setting for coverage check, it could be enabled in IDE itself, but when running coverage in CI/maven/wherever it should be configured in gradle 测试和覆盖率存在问题,因此您需要配置jvmArgs设置以进行覆盖率检查,可以在IDE本身中启用它,但是在CI / maven /中运行覆盖率时,应在gradle中对其进行配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM