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

2022-09-02 01:07:57

我是刚接触过的。我正在使用以下代码。但它为单元测试用例生成覆盖率。但它没有为集成测试用例生成。我在软件包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)
}

答案 1

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

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

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

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

使用Gradle 5.4.1(现在是5.5.1),我可以在任何测试任务之后获得报告,目前我既有任务又有任务。testintegrationTest

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

  • 不要在/块中配置,这是我的部分错误。有关更多信息,请查看此 gradle github 票证executionDatadoLastdoFirst
  • 添加了更谨慎的替代方案(同样不在/块中)doLastdoFirstexecutionData { tasks.withType(Test).findAll { it.jacoco.destinationFile.exists() }*.jacoco.destinationFile }

编辑2:解决方案是一样的,我只是调整了

  • 要使用的报告目标,jacoco.reportsDir
  • 执行数据现在采取,而不仅仅是tasks.withType(Test)[test, integrationTest]
  • 设置在块中完成,而不是executionDatadoFirstdoLast

编辑:在查看了 的文档后,有一个变体 JacoCoReport:executionData 直接接受 Gradle 任务。它之所以有效,是因为JaCoCo插件将JacocoTaskExtension扩展添加到测试类型的所有任务中。这样就不那么容易出错了。JacocoReport


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

较旧但非常有效的答案。同样使用上面的知识(该任务由 扩展),可以替换 by 和 的手动配置。TestJacocoTaskExtensionfileexecutionDatatest.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"

答案 2

我相信最完整的答案会是这样的:

tasks.withType(Test) {
    finalizedBy jacocoTestReport
}

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

    reports {
        csv.enabled true
    }
}

至少它完全适合我的集成和功能测试需求。


推荐