首页 > 解决方案 > jacoco-maven-plugin 不排除测试类

问题描述

该项目的 Jacoco 报告包含测试类的行覆盖率,尽管尝试如下排除测试

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.3</version>
            <configuration>
                <excludes>
                    <exclude>**/*Test.*</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <configuration>
                        <excludes>
                            <exclude>**/*Test.*</exclude>
                        </excludes>
                    </configuration>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <configuration>
                        <excludes>
                            <exclude>**/*Test.*</exclude>
                        </excludes>
                    </configuration>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule implementation="org.jacoco.maven.RuleConfiguration">
                                <excludes>
                                    <exclude>**/*Test.*</exclude>
                                </excludes>
                                <element>CLASS</element>
                                <limits>
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.94</minimum>
                                    </limit>
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.85</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>

上面的 pom 基本上是我们用现有 StackOverflow 问题的解决方案对配置进行射击,但它们都不起作用。

任何反馈将不胜感激!

标签: jacocojacoco-maven-plugin

解决方案


在没有Minimal Complete Reproducible Example的情况下,只能猜测您的测试文件是如何放置在您的项目中以及为什么它们会显示在报告中,因为jacoco-maven-plugin默认情况下不会在报告中显示测试类 - 这是证明:

对于以下pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-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.3</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

src/main/java/Example.java

class Example {
    void sayHello() {
        System.out.println("Hello, World!");
    }
}

src/test/java/ExampleTest.java

import org.junit.Test;

public class ExampleTest {
    @Test
    public void test() {
        new Example().sayHello();
    }
}

执行

mvn clean verify

生成以下报告,如您所见,不包含src/test/java/ExampleTest.java

报告

试图盲目猜测,可以假设您的项目具有非标准结构,其中上述Example.javaExampleTest.java驻留在同一src目录中,并且您有以下pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

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

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

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

  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src</testSourceDirectory>

    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.3</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

然后mvn clean verify确实执行将产生以下报告,其中包含ExampleTest.java

报告

但是在这种情况下,以下排除**/*Test.*

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

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

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

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

  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src</testSourceDirectory>

    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.3</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
              <goal>report</goal>
            </goals>
            <configuration>
              <excludes>
                <exclude>**/*Test.*</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

工作得很好

报告


推荐阅读