jacoco - 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 问题的解决方案对配置进行射击,但它们都不起作用。
任何反馈将不胜感激!
解决方案
在没有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.java
和ExampleTest.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>
工作得很好
推荐阅读
- kubernetes - Kubernetes 入口重定向和重写
- javascript - 使用 ES6 语法解析编译的默认导出
- vuex - Quasar 框架与 VueX
- c# - 如何在 C# 中合并 Excel 包工作表的单元格?
- python - 如果单元格数据与不同 CSV 文件中的另一个单元格匹配,我如何返回单元格行?Python
- java - 如何将唯一数据从 excel 存储到数据库(使用 apache poi)?
- haskell - 括号中逗号分隔的名称是什么意思 - 作为绑定?
- javascript - AmCharts4 比 3 慢很多
- user-interface - JavaFX ChoiceBox 不显示初始值
- docker - 为什么 macos(x86) 可以运行 docker arm 容器 arm64v8/alpine?