Violation Rules¶
Violation rules enforce minimum coverage thresholds on new/modified code.
Quick Setup¶
Enforce 90% coverage on all metrics:
This sets 90% minimum for lines, branches, and instructions, and fails the build if not met.
Coverage Entities¶
Three coverage metrics are available:
| Entity | Description |
|---|---|
LINE |
Source code lines |
BRANCH |
Decision branches (if/else, switch) |
INSTRUCTION |
Bytecode instructions |
Configuring Rules¶
Per-Entity Rules¶
Set different thresholds per entity:
The CLI supports a single --min-coverage threshold applied to all entities:
Per-entity rules
For per-entity thresholds with the CLI, use a configuration file.
Kotlin DSL Shorthand¶
violationRules {
CoverageEntity.LINE {
minCoverageRatio.set(0.8)
}
CoverageEntity.BRANCH {
minCoverageRatio.set(0.7)
}
}
All Entities at Once¶
Fail on Violation¶
Control whether violations fail the build:
Note
failIfCoverageLessThan() automatically sets failOnViolation = true.
Entity Count Threshold¶
Ignore violations if too few entities were changed:
violationRules {
rule(CoverageEntity.BRANCH) {
minCoverageRatio.set(0.8)
entityCountThreshold.set(10) // Ignore if < 10 branches changed
}
}
This is useful for small changes where a single missed branch would cause a large percentage drop.
CLI
Entity count thresholds are not available as CLI flags. Use a configuration file for advanced violation rules.
Example Output¶
Passing¶
Failing¶
> Task :deltaCoverage FAILED
Fail on violations: true. Found violations: 2.
FAILURE: Build failed with an exception.
> java.lang.Exception: Rule violated for bundle test:
instructions covered ratio is 0.5, but expected minimum is 0.9
[test] Rule violated for bundle test:
lines covered ratio is 0.0, but expected minimum is 0.9
Best Practices¶
- Start with 80% and increase gradually
- Use
entityCountThresholdfor branch coverage to avoid noise on small changes - Different thresholds per view — lower for integration tests, higher for unit tests
- Don't set 100% — it's often impractical and leads to low-value tests
Full Example¶
configure<io.github.surpsg.deltacoverage.gradle.DeltaCoverageConfiguration> {
diffSource.git.compareWith("refs/remotes/origin/main")
reportViews {
val test by getting {
violationRules {
failOnViolation.set(true)
rule(CoverageEntity.LINE) {
minCoverageRatio.set(0.85)
}
rule(CoverageEntity.BRANCH) {
minCoverageRatio.set(0.75)
entityCountThreshold.set(5)
}
rule(CoverageEntity.INSTRUCTION) {
minCoverageRatio.set(0.80)
}
}
}
}
}
deltaCoverageReport {
diffSource.git.compareWith('refs/remotes/origin/main')
reportViews {
test {
violationRules {
failOnViolation = true
rule(CoverageEntity.LINE) {
minCoverageRatio = 0.85
}
rule(CoverageEntity.BRANCH) {
minCoverageRatio = 0.75
entityCountThreshold = 5
}
rule(CoverageEntity.INSTRUCTION) {
minCoverageRatio = 0.80
}
}
}
}
}
java -jar delta-coverage-cli.jar \
--diff-file changes.diff \
--engine JACOCO \
--coverage-binary build/jacoco/test.exec \
--classes build/classes/java/main \
--sources src/main/java \
--min-coverage 0.8 \
--fail-on-violation \
--console --html
For per-entity thresholds equivalent to the Gradle config, use a config file: