【问题标题】:Jacoco gradle multi moduleJacoco gradle 多模块
【发布时间】:2017-01-11 02:49:27
【问题描述】:

我们有一个包含多个模块的 gradle 项目 - 所以每个模块 - 例如管理员,最终都会有这样的东西:

task admintest(type: Test) {
    description = "Tests of admin"
    testClassesDir = sourceSets.admintest.output.classesDir
    classpath += sourceSets.admintest.runtimeClasspath
}
check.dependsOn admintest

现在 - 我们正在生成一份 jacoco 报告 - 该报告实际上包括来自所有不同模块的所有类,但仅包含作为主模块一部分运行的测试的 coverage,即“测试”任务 - 当 admintests 和其他人运行时,覆盖率始终为零。如何让 jacoco 也从 admintests 和其他模块测试中获取覆盖范围?

【问题讨论】:

    标签: java unit-testing gradle jacoco


    【解决方案1】:

    找到它 - 对于那些尝试这样做的人 - 秘诀是在 executionData 中指定其他模块测试任务

    例如:

    jacocoTestReport {
        executionData test, admintest, commontest
    }
    

    【讨论】:

    • 这个问题和这个答案有点误导,您的项目设置是一个项目,有多个源集,每个都有一个专用的“测试”任务(这将激活 jacoco 代码覆盖并产生执行数据),但 jacoco 报告是一项必然需要汇总的单一任务。您的回答表明您只需要引用测试任务或源集。
    • 另一个常见的设置是拥有多个子项目而不是单独的源集。在这种情况下,您仍然可以使用根项目中的单个任务生成聚合代码覆盖率报告,您可以通过正确引用子项目来完成类似的操作,或者您可以选择使用 JacocoMerge 任务显式聚合在从聚合执行数据文件创建报告之前,为每个子项目生成的执行数据文件。
    【解决方案2】:

    这是旧线程,但这可能对某人有所帮助:

    我们只在根项目的 build.gradle 中需要这个。

    1. 定义一个公共位置

    2. 也为 sonarqube 提供相同的功能。

       plugins {
           ..
           id "org.sonarqube"
       }
       apply plugin: 'org.sonarqube'
      
       allprojects {
           apply plugin: 'jacoco'
       }
      
       task jacocoRootReport(type: JacocoReport) {
           dependsOn = subprojects.test
           description = 'Generates aggregate report from all subprojects.'
           .....
           reports {
               xml.enabled true
               xml.destination(file("${jacoco.reportsDir}/all-tests/jacocoRootReport.xml"))
               csv.enabled false
           }  
       }
      
       sonarqube {
           properties {
              property "sonar.coverage.jacoco.xmlReportPaths", jacocoRootReport.reports.xml.destination
           }
       }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2017-06-03
      • 2013-03-16
      • 1970-01-01
      • 2020-10-19
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多