SupercovBlog

Guide

Kover exclude: classes, packages and generated code

The filters that work in Kover 0.9.11, and why shop/dto/** and ShopApplication match nothing.

Class names, not file paths. A card: classes("shop/dto/**") excludes nothing; classes("shop.dto.*") and classes("shop.ShopApplicationKt") work.

Kover filters take class names with dots, not file paths: shop.dto.*, not shop/dto/**. A Kotlin file of top-level functions compiles to a class named after the file plus Kt, so exclude shop.ShopApplicationKt.

Exclude patterns at a glance

To excludeWrite in kover { reports { filters { excludes { … } } } }
A packagepackages("shop.dto")
Classes by nameclasses("shop.dto.*")
A file of top-level functionsclasses("shop.ShopApplicationKt")
Classes or functions with an annotationannotatedBy("*Generated")

* matches any characters and ? one. The same filters apply to koverVerify, so the report and the rule agree.

We ran every setup here with Kover 0.9.11, Kotlin 2.4.21 and Gradle 9.7.1 on JDK 21. The project has five classes: Pricing and Receipt have tests; a DTO package, a generated package and the main file have none. Without filters, the report has 8 of 16 lines.

Exclude packages and classes

kover {
    reports {
        filters {
            excludes {
                packages("shop.dto", "shop.generated")
                classes("shop.ShopApplicationKt")
            }
        }
    }
}

packages("shop.dto", "shop.generated") alone left three classes, 7 of 11 lines. classes("shop.dto.*", "shop.ShopApplicationKt") left three as well, 8 of 11: Pricing, Receipt and the generated MoneyKt.

A diagram mapping source files to the classes Kover matches: Pricing.kt to shop.Pricing, dto/OrderDto.kt to shop.dto.OrderDto, ShopApplication.kt to shop.ShopApplicationKt and generated/Money.kt to shop.generated.MoneyKt.
Kover matches compiled class names. Top-level functions live in a class named after the file plus Kt.

Exclude generated code with an annotation

annotation class Generated

@Generated
fun debugDump(value: Any): String {
    val text = value.toString()
    return text
}
kover { reports { filters { excludes { annotatedBy("*Generated") } } } }

Receipt went from 2 of 4 lines to 2 of 2. annotatedBy("shop.Generated") did the same. Kover reads the annotation from the compiled class: with @Retention(AnnotationRetention.SOURCE) the function stayed, at 2 of 4 lines. Kotlin’s default retention, RUNTIME, works.

Why your Kover exclude isn’t working

What we triedWhat happened
classes("shop/dto/**", "shop/ShopApplication*.class")Nothing excluded: Kover matches class names, not paths
classes("shop.ShopApplication") for ShopApplication.ktThe main class stayed: it is shop.ShopApplicationKt
koverVerify with minBound(80) and no filtersFailed at 50%
The same rule with the filters aboveFailed at 77.8%: the filters apply to verify too
A grid of six filters against five classes. No filters keeps all five, 8 of 16 lines. classes with shop.dto.* and the Kt class leaves Pricing, Receipt and MoneyKt, 8 of 11. packages leaves Pricing, Receipt and the Kt class, 7 of 11. Path patterns keep all five. shop.ShopApplication misses the Kt class. annotatedBy keeps every class and drops one method.
What each filter left in the report, measured with Kover 0.9.11.

If you came from JaCoCo, this is the opposite convention: JaCoCo excludes take shop/dto/**, Kover shop.dto.*. See JaCoCo exclude.

What Supercov adds

supercov -- ./gradlew test
supercov runs latest gaps

Supercov runs the same Gradle build and records which test covered each line, with branches and MC/DC, without Kover in the build. On this project it showed that no test tried a large order from a non-member. See Kotlin code coverage for Kover, JaCoCo and Supercov side by side.

More coverage guides