简体   繁体   English

当我的所有测试都在单独的子模块中时,如何使用 gradle 插件获取 Jacoco 覆盖率报告

[英]How do I get a Jacoco coverage report using gradle plugin when all my tests are in a separate submodule

I'm having trouble setting up jacoco coverage for my java project since I'm new to gradle.我在为我的 java 项目设置 jacoco 覆盖时遇到问题,因为我是 gradle 的新手。 My final goal is to connect this to sonarqube.我的最终目标是将其连接到 sonarqube。 All my tests are in a separate module我所有的测试都在一个单独的模块中

structure:结构体:

./build.gradle
settings.gradle

./submodule1/build.gradle
./submodule1/src/main/java/prismoskills/Foo.java

./submodule2/build.gradle
./submodule2/src/main/java/com/project/prismoskills/Bar.java

./test/build.gradle
./test/src/test/java/prismoskills/TestFooBar.java

One way I can think of is to set additionalSourceDirs in test module and enable jacoco only in root and test module.我能想到的一种方法是在 test 模块中设置additionalSourceDirs并仅在 root 和 test 模块中启用 jacoco。

The problem with this approach is that my project has a lot of sub modules(which I haven't shown here) and I am having trouble passing additionalsourcedirs to test module's JacocoReport task in an automated way.这种方法的问题是我的项目有很多子模块(我没有在这里显示)并且我无法以自动化的方式将附加的源目录传递给测试模块的 JacocoReport 任务。

Also it looks like this use case can be handled in maven easily by referring to this https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp Also it looks like this use case can be handled in maven easily by referring to this https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp

Any leads on how to proceed further with gradle will be appreciated.任何有关如何进一步使用 gradle 的线索将不胜感激。 Thanks in advance提前致谢

gradle version: 6.4
jacoco gradle plugin version: 0.8.5

I think the following solution should solve your problem.我认为以下解决方案应该可以解决您的问题。 The idea is that:这个想法是:

  1. JaCoCo exec file is generated for every project为每个项目生成 JaCoCo 执行文件
  2. at the end one XML report with all data is generated最后生成一份包含所有数据的 XML 报告

It does the same for JUnit reports because it is easier to see all tests reports together in the root project instead of navigating between directories.它对 JUnit 报告执行相同的操作,因为在根项目中一起查看所有测试报告而不是在目录之间导航更容易。

plugins {
    id 'base'
    id 'org.sonarqube' version '3.0'
}

allprojects {
    apply plugin: 'jacoco'
    apply plugin: 'project-report'

    // ...
    
    jacoco {
        toolVersion = 0.8.5
    }
}

subprojects {
    // ...

    test {
        reports.html.enabled = false
        useJunitPlatform()
        finalizedBy jacocoTestReport
    }

    jacocoTestReport {
        dependsOn test
        reports.html.enabled = false
    }
}

// ...

task testReport(type: TestReport) {
    destinationDir = file("${buildDir}/reports/test")
    reportOn subprojects*.test
}
task jacocoTestReport(type: JacocoReport) {
    subprojects { subproject ->
        subproject.tasks.findAll { it.extensions.findByType(JacocoTaskExtension) }.each { extendedTask ->
            configure {
                sourceSets subproject.sourceSets.main
                if (file("${subproject.buildDir}/jacoco/${extendedTask.name}.exec").exists()) {
                    executionData(extendedTask)
                }
            }
        }
    }
    reports.xml.enabled = true
}
rootProject.getTasksByName('test', true).each {
    it.finalizedBy(testReport)
    it.finalizedBy(jacocoTestReport)
}

This line这条线

if (file("${subproject.buildDir}/jacoco/${extendedTask.name}.exec").exists()) {

is added to prevent build failures when some subprojects don't have tests at all.添加以防止在某些子项目根本没有测试时构建失败。

Following can be defined in the build.gradle of root project.以下可以在根项目的 build.gradle 中定义。 Jacoco plugin will be applied to all the submodules. Jacoco 插件将应用于所有子模块。

subprojects{
  plugins{
    id 'java'
    id 'jacoco'
  }
  test.finalizedBy jacocoTestReport
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Android gradle 插件 0.10.0 或更高版本获取 jacoco 覆盖率报告? - How do I get a jacoco coverage report using Android gradle plugin 0.10.0 or higher? 使用gradle生成单个单元测试的Jacoco代码覆盖率报告 - Generate Jacoco code coverage report for individual unit tests using gradle 如何使用新的Gradle SonarQube插件向Jason报告Jacoco Groovy代码覆盖率? - How to report Jacoco Groovy code coverage to Sonar using new Gradle SonarQube plugin? 具有多个子模块的 Gradle jacoco 覆盖率报告? - Gradle jacoco coverage report with more than one submodule(s)? 如何在 gradle 项目中获取 jacoco 代码覆盖率报告 - How to get jacoco code coverage report in gradle project Gradle:如何使用 jacoco 生成集成测试的覆盖率报告 - Gradle : How to generate coverage report for Integration test using jacoco 带有 Jacoco 插件的 build.gradle 不会为集成测试生成覆盖率报告 - build.gradle with Jacoco plugin doesn't generate coverage report for integration tests 在带有Gradle的Android上使用Jacoco生成覆盖率报告时如何排除测试 - How to exlude tests when generating coverage reports using Jacoco on Android with gradle Gradle 跳过jacoco覆盖插件 - Gradle skipping jacoco coverage plugin 如何使用gradle在mutli模块系统中使用声纳+ jacoco生成集成测试的代码覆盖率报告 - How to generate code coverage report for integration test using sonar + jacoco in mutli module system using gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM