gpt4 book ai didi

java - Gradle : How to generate coverage report for Integration test using jacoco

转载 作者:行者123 更新时间:2023-12-02 01:56:26 28 4
gpt4 key购买 nike

我是 gradle 新手。我正在使用下面的代码。但它会生成单元测试用例的覆盖范围。但它没有生成集成测试用例。我的测试类位于包 src/test/java 中。

test {
dependsOn jettyRunWar
ignoreFailures true
finalizedBy jettyStop
}

apply plugin: 'jacoco'

jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
}

最佳答案

编辑4:Gradle 7.4 RC1 release notes表示 gradle 现在可以为 JUnit 和 JaCoCo 生成单个报告文件。这将避免下面解释的脆弱配置。

您所要做的就是应用相关插件

目前的缺点(7.4 RC1)是仅支持 HTML 报告。这些聚合任务与 JVM test suite plugin 协同工作(但由 java 插件自动添加)。

因此,请关注下一版本中的此功能。

使用 Gradle 5.4.1(现在是 5.5.1),我能够在任何测试任务后获得报告,目前我有 testintegrationTest任务。

EDIT3:修复了仅执行某些测试任务时的潜在错误

  • 不要配置executionDatadoLast/doFirst block ,这是我的错误。欲了解更多信息,请查看此gradle github ticket
  • 添加了更谨慎的替代方案(同样不在 doLast/doFirst block 中) executionData { tasks.withType(Test).findAll { it.jacoco.destinationFile.exists() }*.jacoco.destinationFile }

EDIT2:解决方案是相同的,我只是调整了

  • 要使用的报告目标 jacoco.reportsDir ,
  • executionData 现在采用 tasks.withType(Test)而不仅仅是[test, integrationTest]
  • 设置executionData是在 doFirst 中完成的阻止而不是 doLast

编辑:查看 JacocoReport 的文档后,有一个变体 JacocoReport:executionData直接接受 Gradle 任务。它有效是因为 the JaCoCo plugin adds a JacocoTaskExtension extension to all tasks of type Test 。这样就不容易出错。

<小时/>
jacocoTestReport {
// The JaCoCo plugin adds a JacocoTaskExtension extension to all tasks of type Test.
// Use task state to include or not task execution data
// https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskState.html
// This declaration will be used as a closure, notice there no wrapping parenthesis
executionData tasks.withType(Test).findAll { it.state.executed }

// If the above instruction really don't work, there maybe some things that intervene in the process, in this case, you may be a bit more lucky with this instruction
// executionData { tasks.withType(Test).findAll { it.jacoco.destinationFile.exists() }*.jacoco.destinationFile }

reports {
xml.enabled true
xml.destination(file("${jacoco.reportsDir}/all-tests/jacocoAllTestReport.xml"))
html.enabled true
html.destination(file("${jacoco.reportsDir}/all-tests/html"))
}
}

同样的技巧可以应用于 sonarqube任务:

sonarqube {
group = "verification"
properties {
// https://jira.sonarsource.com/browse/MMF-1651
property "sonar.coverage.jacoco.xmlReportPaths", jacocoTestReport.reports.xml.destination
properties["sonar.junit.reportPaths"] += integrationTest.reports.junitXml.destination
properties["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs
// ... other properties
}
}
<小时/>

较旧但非常有效的答案。另外,利用上述知识( Test 的任务由 JacocoTaskExtension 扩展),可以替换手册 fileexecutionData的配置通过test.jacoco.destinationFileintegrationTest.jacoco.destinationFile .

// Without it, the only data is the binary data, 
// but I need the XML and HTML report after any test task
tasks.withType(Test) {
finalizedBy jacocoTestReport
}

// Configure the report to look for executionData generated during the test and integrationTest task
jacocoTestReport {
executionData(file("${project.buildDir}/jacoco/test.exec"),
file("${project.buildDir}/jacoco/integrationTest.exec"))
reports {
// for sonarqube
xml.enabled true
xml.destination(file("${project.buildDir}/reports/jacoco/all-tests/jacocoAllTestReport.xml"))
// for devs
html.enabled true
html.destination file("${project.buildDir}/reports/jacoco/all-tests/html")
}
}


sonarqube {
group = "verification"
properties {
// https://jira.sonarsource.com/browse/MMF-1651
property "sonar.coverage.jacoco.xmlReportPaths", ${project.buildDir}/test-results/integrationTest"
properties["sonar.junit.reportPaths"] += "${project.buildDir}/test-results/integrationTest"
properties["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs
// ... other properties
}
}

project.tasks["sonarqube"].dependsOn "jacocoTestReport"

关于java - Gradle : How to generate coverage report for Integration test using jacoco,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025138/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com