Gradle:如何使用 jacoco 为集成测试生成覆盖率报告

新手上路,请多包涵

我是新手。我正在使用下面的代码。但它会生成单元测试用例的覆盖率。但它没有为集成测试用例生成。我的测试类在包 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)
}

原文由 Veera 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
1 个回答

EDIT4Gradle 7.4 RC1 发行说明 表明 gradle 现在可以为 JUnit 和 JaCoCo 生成单个报告文件。这将避免下面解释的脆弱配置。

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

目前的缺点 (7.4 RC1) 是只支持 HTML 报告。并且这些聚合任务与 JVM 测试套件插件 协同工作(但由 java 插件自动添加)。

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

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

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

  • 不要在 doLast / doFirst 块中配置 executionData 块,这是我的错误。有关更多信息,请查看此 gradle github 票证
  • 添加了更谨慎的选择(再次不在 doLast / doFirst 块中) 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 任务。它的工作原理是因为 JaCoCo 插件添加了一个 JacocoTaskExtension 扩展到所有类型的任务 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
    }
}


较旧但非常有效的答案。 Also using the knowledge above (that Test s task are extended by JacocoTaskExtension ) it’s possible to replace the manual file configuration of executionData by 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"

原文由 Brice 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题