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

Vee*_*era 11 java 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)
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*ice 16

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

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

  • 不要executionDatadoLast/doFirst块中配置,这是我的错误。有关更多信息,请查看此gradle github 票证
  • 添加了更谨慎的选择(再次不在doLast/doFirst块中) executionData { tasks.withType(Test).findAll { it.jacoco.destinationFile.exists() }*.jacoco.destinationFile }

EDIT2:解决方案是一样的,我只是调整

  • 要使用的报告目的地jacoco.reportsDir
  • executionData 现在需要,tasks.withType(Test)而不仅仅是[test, integrationTest]
  • 设置executionDatadoFirst块中完成而不是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"))
    }
}
Run Code Online (Sandbox Code Playgroud)

同样的技巧可以应用于sonarqubetask :

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
    }
}
Run Code Online (Sandbox Code Playgroud)

较旧但非常有效的答案。同样使用上述知识(该Test任务由 扩展JacocoTaskExtension),可以替换由和的手动file配置。executionDatatest.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"
Run Code Online (Sandbox Code Playgroud)

  • 较新的解决方案使用替代的executionData指令:`executionData {tasks.withType(Test).findAll { it.jacoco.destinationFile.exists() }*.jacoco.destinationFile }` (2认同)
  • 旧版本有效,谢谢:)只需要这个小修复:属性“sonar.coverage.jacoco.xmlReportPaths”,“${project.buildDir}/reports/jacoco/all-tests/jacocoAllTestReport.xml” (2认同)

use*_*882 6

我相信最完整的答案如下:

tasks.withType(Test) {
    finalizedBy jacocoTestReport
}

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

    reports {
        csv.enabled true
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Leo*_*ber 6

由于我无法使用任何答案运行它,因此我将在此处添加我的解决方案。integTest如果您首先运行测试任务(例如)然后调用,它将起作用jacocoTestReport

您所需要的只是告诉jacocoTestReport任务在哪里可以找到从测试任务中收集的执行数据。执行数据始终以测试任务命名。因此,如果您有一个名为的测试任务integTest,您的执行数据将存储在build/jacoco/integTest.exec. 通过将任务jacocoTestReport添加到属性executionData,也可以将任务配置为查找其他文件。您还可以添加通配符,以便考虑所有执行数据:

jacocoTestReport {
    executionData = fileTree(dir: project.projectDir, includes: ["**/*.exec"])
}
Run Code Online (Sandbox Code Playgroud)

更新 正如 @rahulmohan 所说,executionData 属性已变为只读。相反,定义 jacocoTestReport 任务如下:

jacocoTestReport {
    getExecutionData().from(fileTree(project.projectDir).include("/jacoco/*.exec"))
Run Code Online (Sandbox Code Playgroud)

通过执行下面的语句,将为您创建集成测试任务的测试覆盖 jacoco 报告(例如integTest

./gradlew integTest jacocoTestReport
Run Code Online (Sandbox Code Playgroud)

这也适用于您想要integTest在 module 中运行任务的多模块项目a

./gradlew a:integTest a:jacocoTestReport
Run Code Online (Sandbox Code Playgroud)

  • executionData 是一个只读字段。我收到错误:“无法为 org.gradle.testing.jacoco.tasks.JacocoReport 类型的任务 ':jacocoTestReport' 设置只读属性 'executionData' 的值” (2认同)

Aru*_*gal 4

看起来,您需要告诉 build.gradle 的是使用 sourceSets 的集成测试(即包含这些 IT 测试的文件夹)在哪里。就我而言,我的源代码位于 src/java 下(而不是 src/main/java - gradle 默认值)。我的单元测试(Junit)位于 test/java 文件夹下,集成测试位于 src/java-test 文件夹下。

sourceSets {
   main {
      java {
         srcDir 'src/java'
      }
   }
   test {
      java {
         srcDir 'test/java'
      }
      resources {
         srcDir 'test/resources'
         srcDir 'conf'
      }
   }
   integrationTest {
      java {
         srcDir 'src/java-test'
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

然后,我有integrationTest任务...您可以调整它,因为您可能没有cleanTest(我创建的自定义任务),所以您可以忽略dependsOn...我认为在您的情况下您将使用类似jettyStart的东西当您使用它进行 IT 测试时(启动容器来运行 IT 测试,然后使用 FinalizedBy 功能来停止 jetty .. jetty 插件)

task integrationTest( type: Test, dependsOn: cleanTest ) {
   jacoco {
      //destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
      destinationFile = file("$buildDir/jacoco/integrationTest.exec")
      //classDumpFile = file("$buildDir/jacoco/classpathdumps")
      classDumpFile = file("$buildDir/classes/integrationTest")
   }
   testClassesDir = sourceSets.integrationTest.output.classesDir
   classpath = sourceSets.integrationTest.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)

请参阅这篇文章,了解我最后的更详细的输出结构和脚本。我得到了单元测试 (test.exec) 和 IT 测试 intergrationTest.exec 的 .exec ..但我没有得到这两个测试的 jacoco.xml/jacocoHtml 报告。我还发现,如果我运行“gradle clean build”(其中包括对“test”任务的调用)和“gradle clean build IntegrationTest”,那么稍后会覆盖 build/test-results 文件夹和 build/reports/ 中的单元测试数据测试文件夹。

Jacoco 单元和集成测试覆盖范围 - 个人和整体

注意:在我的例子中,jacocoTestReport 是在通用 gradle 文件之一的全局 gradle init.d 文件夹中定义的。这将帮助我们不在所有/项目级别的 build.gradle 文件中包含相同的代码。