Configure Jacoco with gradle and kotlin DSL
Asked Answered
E

3

7

I'm trying to configure Jacoco to exclude some classes from analysis but can't find any working example :(

I found some samples with afterEvaluate but no success

Eurhythmics answered 9/5, 2019 at 9:21 Comment(2)
Please add an example on what you tried or your current build.gradle file and add details what exactly does not work as you expect.Temuco
All that i've tried, don't even compile. So i've no relevant example to provideEurhythmics
I
9

src/main/java/org/example/A.java:

package org.example;

class A {
}

src/main/java/org/example/B.java:

package org.example;

class B {
}

src/test/java/org/example/ExampleTest.java:

package org.example;

public class ExampleTest {
  @org.junit.Test
  public void test() {
    new A();
    new B();
  }
}

build.gradle.kts:

plugins {
  java
  jacoco
}

repositories {
  mavenCentral()
}

dependencies {
  testCompile("junit:junit:4.12")
}

using Gradle 5.4.1 execution of gradle test jacocoTestReport produces following report

report

after addition to build.gradle.kts

tasks.withType<JacocoReport> {
  classDirectories.setFrom(
    sourceSets.main.get().output.asFileTree.matching {
      exclude("org/example/B.class")
    }
  )
}

execution of the same command produces following report

report after exclusion

Inflict answered 9/5, 2019 at 10:29 Comment(0)
C
6

Just to add to @Godin's awesome answer:

The way @Godin explained it, you would have to run gradle test jacocoTestReport which isn't bad but If you want jacoco to run when you run just with gradle test add this to your build.gralde.kts:

tasks.test {
    finalizedBy("jacocoTestReport")
    doLast {
        println("View code coverage at:")
        println("file://$buildDir/reports/jacoco/test/html/index.html")
    }
}
Cauchy answered 11/8, 2019 at 15:19 Comment(0)
F
5

I've managed to exclude this way:

tasks.jacocoTestReport {
    classDirectories.setFrom(
        files(classDirectories.files.map {
            fileTree(it) {
                exclude(
                    "com/example/integration/**",
                    "com/example/application/*Ext*"
                )
            }
        })
    )
}

Taken from here

Fruitarian answered 30/9, 2021 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.