Configure jacocoTestReport to read multiple .exec files as input
Asked Answered
L

4

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?

Licko answered 7/4, 2020 at 0:11 Comment(0)
D
14

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
    }
}
Donohue answered 9/4, 2020 at 16:31 Comment(0)
A
14

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

Amphitrite answered 15/1, 2021 at 16:0 Comment(0)
M
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

Matty answered 16/9, 2021 at 15:3 Comment(0)
G
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)
}
Godfrey answered 29/9, 2022 at 15:9 Comment(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.Gallion

© 2022 - 2024 — McMap. All rights reserved.