7

In my gradle build I have 2 test tasks like this:

task testAAA(type: Test) {
    filter {

        includeTestsMatching "*AAA*"
    }

    finalizedBy jacocoTestReport
}

and

task testBBB(type: Test) {
    filter {

        includeTestsMatching "*BBB*"
    }

    finalizedBy jacocoTestReport
}

This generates 2 .exec files in build/jacoco:

  • testAAA.exec

  • testBBB.exec

I want to generate a single coverage report that takes input from BOTH/ALL of the .exec files, I tried this:

jacocoTestReport {
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    reports {
        xml.enabled true
    }

}

When I try that I get this error:

Execution failed for task ':Project1:jacocoTestReport'.
> Unable to read execution data file Project1/build/jacoco/test.exec

Project1/build/jacoco/test.exec (No such file or directory)

Why is jacocoTestReport looking for "test.exec" when I explicitly provided an executionData specification?

MattG
  • 5,589
  • 5
  • 36
  • 52

4 Answers4

12

I struggled with this for a while and even had success. Until I came back to it yesterday. Spent a few hours searching and found this on GH.

jacocoTestReport {
  getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec")) 
}

As of Gradle 6.0 this is the route to go. Have tested it against a repo that has 2 sets of tests and I can run either separately or both at once and Jacoco doesn't blow up.

Jacoco JavaDocs
GH Issue with solution

JM_24
  • 147
  • 1
  • 12
11

I would recommend passing in the test tasks instead of a file tree. This will allow the plugin to make sure the correct files are looked up and will resolve some execution ordering problems that could happen, like making sure this report tasks runs after the test tasks themselves.

So something like:

jacocoTestReport {
    executionData tasks.withType(Test)

    reports {
        xml.enabled true
    }
}
Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
2

The predefined JacocoReport task whose name is jacocoTestReport will be set an execution data file by default, whose name is "test.exec".

So, you can try the following code:

task testAAAReport(type: JacocoReport) {
    sourceSets sourceSets.main

    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    reports {
        xml.enabled true
    }

}

the source code

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Krivers
  • 41
  • 1
  • 6
1

build.gradle.kts

tasks.jacocoTestReport {
    executionData.setFrom(fileTree(buildDir).include("/jacoco/*.exec"))
    classDirectories.setFrom(sourceSets.main.get().output.asFileTree)
    reports {
        xml.required.set(true)
        html.required.set(true)
    }
    finalizedBy(tasks.jacocoTestCoverageVerification)
}

tasks.jacocoTestCoverageVerification {
    executionData.setFrom(fileTree(buildDir).include("/jacoco/*.exec"))
    classDirectories.setFrom(sourceSets.main.get().output.asFileTree)
}
Tutio
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '22 at 08:56