Exclusions from Jacoco Report – Jacoco报告中不包括的内容

最后修改: 2021年 6月 4日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll learn how to exclude certain classes and packages from JaCoCo test coverage reports.

在本教程中,我们将学习如何将某些类和包从JaCoCo测试覆盖率报告中排除。

Generally, the candidates for exclusion can be configuration classes, POJOs, DTOs, as well as generated byte code. These carry no specific business logic, and it could be useful to exclude them from the reports in order to provide a better view of the test coverage.

一般来说,排除的候选者可以是配置类、POJO、DTO,以及生成的字节码。这些没有具体的业务逻辑,为了提供更好的测试覆盖率,将它们从报告中排除可能是有用的。

We’ll explore various ways of exclusion in both Maven and a Gradle project.

我们将探讨Maven和Gradle项目中的各种排除方法。

2. Example

2.例子

Let’s start with a sample project where we have all the required code already covered by tests.

让我们从一个样本项目开始,在这个项目中,我们有所有需要的代码已经被测试覆盖。

Next, we’ll generate the coverage report by running mvn clean package or mvn jacoco:report:

接下来,我们将通过运行mvn clean packagemvn jacoco:report来生成覆盖报告。

This report shows that we already have the required coverage, and missed instructions should be excluded from JaCoCo report metrics.

这份报告显示,我们已经有了所需的覆盖率,遗漏的指令应该被排除在JaCoCo报告的指标之外。

3. Excluding Using Plugin Configuration

3.使用插件配置排除

Classes and packages can be excluded using standard * and ? wildcard syntax in the plugin configuration:

类和包可以在插件配置中使用标准的*和?通配符语法排除。

  • * matches zero or more characters
  • ** matches zero or more directories
  • ? matches a single character

3.1. Maven Configuration

3.1.Maven配置

Let’s update the Maven plugin to add several excluded patterns:

让我们更新Maven插件,增加几个排除模式。

<plugin> 
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>com/baeldung/**/ExcludedPOJO.class</exclude>
            <exclude>com/baeldung/**/*DTO.*</exclude>
            <exclude>**/config/*</exclude>
        </excludes>
     </configuration>
     ...
</plugin>

Here we’ve specified the following exclusions:

在这里,我们指定了以下排除项。

  • ExcludedPOJO class in any sub-package under com.baeldung package
  • all classes with names ending in DTO in any sub-package under com.baeldung package
  • the config package declared anywhere in the root or sub-packages

3.2. Gradle Configuration

3.2.Gradle配置

We can also apply the same exclusions in a Gradle project.

我们也可以在Gradle项目中应用同样的排除法。

First, we’ll update the JaCoCo configuration in build.gradle and specify a list of exclusion, using the same patterns as earlier:

首先,我们将更新build.gradle中的JaCoCo配置,并指定一个排除列表,使用与前面相同的模式。

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
    
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: [
                "com/baeldung/**/ExcludedPOJO.class",
                "com/baeldung/**/*DTO.*",
                "**/config/*"
            ])
        }))
    }
}

We use a closure to traverse the class directories and eliminate files that match a list of specified patterns. As a result, generating the report using ./gradlew jacocoTestReport or ./gradlew clean test will exclude all the specified classes and packages, as expected.

我们使用一个闭包来遍历类目录,并消除符合指定模式列表的文件。因此,使用./gradlew jacocoTestReport./gradlew clean test生成的报告将排除所有指定的类和包,正如预期的那样。

It’s worth noting that the JaCoCo plugin is bound to the test phase here, which runs all the tests prior to generating the reports.

值得注意的是,JaCoCo插件在这里被绑定到test阶段,它在生成报告之前运行所有测试。

4. Excluding With Custom Annotation

4.用自定义注释排除

Starting from JaCoCo 0.8.2, we can exclude classes and methods by annotating them with a custom annotation with the following properties:

从JaCoCo 0.8.2开始,我们可以custom annotation注解类和方法来排除它们,其属性如下。

  • The name of the annotation should include Generated.
  • The retention policy of annotation should be runtime or class.

First, we’ll create our annotation:

首先,我们将创建我们的注释。

@Documented
@Retention(RUNTIME)
@Target({TYPE, METHOD, CONSTRUCTOR})
public @interface Generated {
}

Now we can annotate class(es) or method(s) or constructor(s) that should be excluded from the coverage report.

现在我们可以注释那些应该从覆盖率报告中排除的类、方法或构造函数。

Let’s use this annotation at the class level first:

让我们先在类的层面上使用这个注解。

@Generated
public class Customer {
    // everything in this class will be excluded from jacoco report because of @Generated
}

Similarly, we can apply this custom annotation to a specific method in a class:

同样地,我们可以将这个自定义注解应用于一个类中的特定方法。

public class CustomerService {

    @Generated
    public String getCustomerId() {
        // method excluded form coverage report
    }
    
    public String getCustomerName() {
        // method included in test coverage report
    }
}

Finally, let’s apply the annotation to the constructor:

最后,让我们把注解应用到构造函数中。

public class CustomerService {
    
    @Generated
    public CustomerService(){
        //constructor excluded from coverage report
    }

}

5. Excluding Lombok Generated Code

5.排除Lombok生成的代码

Project Lombok is a popular library for greatly reducing boilerplate and repetitive code in Java projects.

Project Lombok是一个流行的库,用于大大减少Java项目中的模板和重复的代码。

Let’s see how to exclude all Lombok-generated bytecode by adding a property to lombok.config file in our project’s root directory:

让我们看看如何通过在项目根目录下的lombok.config文件中添加一个属性来排除所有Lombok生成的字节码

lombok.addLombokGeneratedAnnotation = true

Basically, this property adds the lombok.@Generated annotation to the relevant methods, classes, and fields of all the classes annotated with Lombok annotations, e.g. the Product class. As a result, JaCoCo then ignores all the constructs annotated with this annotation, and they’re not shown in the reports.

基本上,这个属性将lombok.@Generated注解添加到所有用Lombok注解的类的相关方法、类和字段,例如Product类。结果,JaCoCo就会忽略所有用这个注解的构造,它们就不会显示在报告中。

Finally, we can see the report after applying all the exclusion techniques shown above:

最后,我们可以看到应用上述所有排除法技术后的报告。

6. Conclusion

6.结语

In this article, we demonstrated various ways of specifying exclusions from the JaCoCo test report.

在这篇文章中,我们演示了从JaCoCo测试报告中指定排除的各种方法。

Initially, we excluded several files and packages using naming patterns in the plugin configuration. Then we saw how to use @Generated to exclude certain classes, as well as methods. Finally, we learned how to exclude all the Lombok-generated code from the test coverage report using a config file.

最初,我们使用插件配置中的命名模式排除了几个文件和包。然后我们看到如何使用@Generated来排除某些类和方法。最后,我们学习了如何使用一个配置文件从测试覆盖率报告中排除所有Lombok生成的代码。

As always, the Maven source code and Gradle source code are available over on Github.

一如既往,Maven源代码Gradle源代码都可以在Github上找到。