配置jacocoTestReport以读取多个 .exec 文件作为输入。

huangapple go评论70阅读模式
英文:

Configure jacocoTestReport to read multiple .exec files as input

问题

在我的Gradle构建中,我有两个类似这样的测试任务:

```groovy
task testAAA(type: Test) {
    filter {
        includeTestsMatching "*AAA*"
    }
    finalizedBy jacocoTestReport
}

以及

task testBBB(type: Test) {
    filter {
        includeTestsMatching "*BBB*"
    }
    finalizedBy jacocoTestReport
}

这将在build/jacoco目录中生成两个.exec文件:

  • testAAA.exec
  • testBBB.exec

我希望生成一个汇总的覆盖率报告,该报告从所有.exec文件中获取输入。我尝试了以下方式:

jacocoTestReport {
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
    reports {
        xml.enabled true
    }
}

但是当我尝试时,我得到了以下错误:

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)

为什么jacocoTestReport在明确提供了executionData规范时还会寻找"test.exec"文件呢?


<details>
<summary>英文:</summary>

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

task testAAA(type: Test) {
filter {

    includeTestsMatching &quot;*AAA*&quot;
}

finalizedBy jacocoTestReport

}


and

task testBBB(type: Test) {
filter {

    includeTestsMatching &quot;*BBB*&quot;
}

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 &quot;test.exec&quot; when I explicitly provided an executionData specification?

</details>


# 答案1
**得分**: 12

```plaintext
我曾经为此苦苦挣扎,甚至取得了一些成功。直到昨天我回头再次处理它。花了几个小时的时间进行搜索,发现了在 GH 上的这个解决方法。

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

自 Gradle 6.0 起,这就是要走的路。我已经在一个包含两套测试的存储库上进行了测试,我可以分别运行它们,也可以同时运行两套测试,而 Jacoco 不会出问题。

[Jacoco JavaDocs][1]
[带有解决方案的 GH 问题][2]

  [1]: https://docs.gradle.org/6.8/javadoc/org/gradle/testing/jacoco/tasks/JacocoReportBase.html
[2]:
https://github.com/gradle/gradle/issues/5898
英文:

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(&quot;/jacoco/*.exec&quot;)) 
}

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

答案2

得分: 11

我建议传入测试任务而不是文件树。这将允许插件确保正确查找文件,并解决一些可能出现的执行顺序问题,比如确保此报告任务在测试任务本身之后运行。

所以可以像这样:

jacocoTestReport {
    executionData tasks.withType(Test)

    reports {
        xml.enabled true
    }
}
英文:

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

答案3

得分: 2

以下是翻译好的内容:

"jacocoTestReport"任务是预定义的 JacocoReport 任务,它默认会设置一个执行数据文件,文件名为 "test.exec"。

因此,你可以尝试以下代码:

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

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

    reports {
        xml.enabled true
    }

}

源代码链接

英文:

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(&quot;**/build/jacoco/*.exec&quot;)

    reports {
        xml.enabled true
    }

}

the source code

答案4

得分: 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)
}
英文:

build.gradle.kts

tasks.jacocoTestReport {
    executionData.setFrom(fileTree(buildDir).include(&quot;/jacoco/*.exec&quot;))
    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(&quot;/jacoco/*.exec&quot;))
    classDirectories.setFrom(sourceSets.main.get().output.asFileTree)
}

huangapple
  • 本文由 发表于 2020年4月7日 08:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定