MattG
MattG

Reputation: 6445

Configure jacocoTestReport to read multiple .exec files as input

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:

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?

Upvotes: 7

Views: 7782

Answers (4)

Tutio
Tutio

Reputation: 11

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)
}

Upvotes: 1

Krivers
Krivers

Reputation: 59

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

Upvotes: 2

JM_24
JM_24

Reputation: 159

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

Upvotes: 14

Louis Jacomet
Louis Jacomet

Reputation: 14500

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
    }
}

Upvotes: 14

Related Questions