jacoco - 如何为 'jacoco-check' 指定特定的包?

标签 jacoco jacoco-maven-plugin

我的项目是使用 Maven 构建的。我使用“Jacoco”插件来执行质量检查。

对于一个项目,我想在线检查测试覆盖率。我只想检查仅 3 个包裹的线路覆盖率。如何指定此检查?

我尝试“包括”一些软件包,但这不起作用。
我还尝试包含根包级别并排除许多其他包。也不工作。

我如何检查包裹 A、B 和 C?请参阅下面的示例:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.9</version>
    <executions>
      ...
      <execution>
        <id>jacoco-check</id>
        <goals>
          <goal>check</goal>
        </goals>
        <configuration>
          <rules>
            <rule>
              <element>PACKAGE</element>
              <includes>
                <include>nl.abc.xyz.package-a.**</include>
                <include>nl.abc.xyz.package-b.**</include>
                <include>nl.abc.xyz.package-c.**</include>
              </includes>
              ... 
              <limits>
                <limit>
                  <counter>LINE</counter>
                  <value>COVEREDRATIO</value>
                  <minimum>0.30</minimum>
                </limit>
              </limits>
            </rule>
          </rules>
        </configuration>
      </execution>
    </executions>
  </plugin>

最佳答案

includesexcludesrule都是关于name对应的元素。
如果是 <element>PACKAGE</element>它们是关于包名的。
因此

          <includes>
            <include>nl.abc.xyz.package-a.**</include>
            <include>nl.abc.xyz.package-b.**</include>
            <include>nl.abc.xyz.package-c.**</include>
          </includes>


匹配例如名为 nl.abc.xyz.package-a.something 的包,但不匹配 nl.abc.xyz.package-a .

给定的
src/main/java/org/example/a/A.java
package org.example.a;

public class A {
}
src/main/java/org/example/a/B.java
package org.example.b;

public class B {
}
src/test/java/ExampleTest.java
public class ExampleTest {
  @org.junit.Test
  public void test() {
    new org.example.a.A();
  }
}

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.2</version>
        <executions>
          <execution>
            <id>prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
          <execution>
            <id>check</id>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <rules>
                <rule>
                  <element>PACKAGE</element>
                  <includes>
                    <include>org.example.b</include>
                  </includes>
                  <limits>
                    <limit>
                      <counter>LINE</counter>
                      <value>COVEREDRATIO</value>
                      <minimum>0.90</minimum>
                    </limit>
                  </limits>
                </rule>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

执行 mvn verify会按预期失败
[INFO] --- jacoco-maven-plugin:0.8.2:check (check) @ example ---
[INFO] Loading execution data file /private/tmp/j/target/jacoco.exec
[INFO] Analyzed bundle 'example' with 2 classes
[WARNING] Rule violated for package org.example.b: lines covered ratio is 0.00, but expected minimum is 0.90
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

和更换后 <include>org.example.b</include><include>org.example.*</include>也会失败并显示相同的消息,因为 org.example.*匹配 org.example.b .更换后 <include>org.example.a</include>会按预期成功。

enter image description here

关于jacoco - 如何为 'jacoco-check' 指定特定的包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54055625/

相关文章:

java - JaCoCo 报告在 Jenkins : java. lang.ArrayIndexOutOfBoundsException 中失败

java - 如何从 jacoco 报告(Instrumentation offline)中正确排除和包含类、包和 jar 类、lib

android - Gradle 中已弃用的 JaCoCo 扩展的替代品是什么?

java - 了解 JaCoCo 覆盖率 XML 报告

playframework - Sonar-Runner没有索引任何文件

java - Jacoco 仅在我的系统上失败,并出现有关 StringUtils 的奇怪错误

code-coverage - 在 JaCoCo Code Coverage 中排除 Setter 和 Getter

maven - 受 AspectJ 编译时织入影响的 Jacoco 代码覆盖率

maven - Jacoco Maven插件-生命周期配置未涵盖插件执行