1
votes

I am using the gradle jacoco plugin to get by coverage reports for tests and integration tests. Sources for the integration tests are defined with the test-sets plugin. To get both tests and integration tests covered in the report, I deduced from this post that I should call executionData with an include on *.exec files. Now, I am trying to figure out how I can exclude a single class from the jacoco report (and analysis). Filter JaCoCo coverage reports with Gradle points to using afterEvaluate and alter the classDirectories, but this generates an empty report when I use it.

plugins {
    id 'org.unbroken-dome.test-sets' version '1.5.0'
}

apply plugin: 'java'
apply plugin: 'jacoco'

sourceSets {
    main {
        java {
            srcDirs = ['jsrc']
        }
    }
}

testSets {
    integrationTest {
        dirName = 'integration-test'
    }
}

jacocoTestReport {
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
    // commented because causes the report to be empty
    //afterEvaluate {
    //    classDirectories = files(classDirectories.files.collect {
    //        fileTree(dir: it, exclude: ['**/TheOneClassToExclude.*'])
    //    })
    //}
}

In case this matters, I am using gradle 4.4 with JDK7.

1

1 Answers

0
votes

After digging into it some more:

  • I realized class files in build/classes/java were being wiped out after every jacocoTestReport run.
  • Looking more closely how executionData was used in the post I mentioned, I decided to explicitly define the jacocoTestReport source set: sourceSets sourceSets.main which addressed nearly all issues I had. Files in build/classes were no longer wiped.
  • the last issue I faced was caused by my exclude rule not excluding the specific class I wanted. I specified the whole path to the package instead (that package contains only class in my case) and that also addressed the problem.

Fixed build.gradle:

plugins {
    id 'org.unbroken-dome.test-sets' version '1.5.0'
}

apply plugin: 'java'
apply plugin: 'jacoco'

sourceSets {
    main {
        java {
            srcDirs = ['jsrc']
        }
    }
}

testSets {
    integrationTest {
        dirName = 'integration-test'
    }
}

jacocoTestReport {
    sourceSets sourceSets.main    // important!
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: ['**/path/to/package/*'])
        })
    }
}