Home > OS >  Exclude Kotlin data classes from JaCoCo test coverage
Exclude Kotlin data classes from JaCoCo test coverage

Time:07-30

I have the latest version of JaCoCo with Gradle(the latest version). How can I exclude data classes from test coverage?

CodePudding user response:

Starting from JaCoCo v0.8.2, you can exclude classes and methods by using a Generated annotation, JaCoCo will ignore them.

@ExcludeGenerated
data class User(val id: Int)

class Something {
    @ExcludeGenerated
    fun ignoreMe() { }
}
@Retention(AnnotationRetention.RUNTIME)
@Target(
    AnnotationTarget.CLASS,
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER,
    AnnotationTarget.CONSTRUCTOR
)
annotation class ExcludeGenerated

https://github.com/jacoco/jacoco/releases/tag/v0.8.2

Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).

CodePudding user response:

In case you are using gradle config:

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

Refer the following for more context:
https://www.baeldung.com/jacoco-report-exclude

  • Related