【问题标题】:gradle jacocoTestReport is not working?gradle jacocoTestReport 不工作?
【发布时间】:2015-05-19 18:45:12
【问题描述】:

我尝试使用 gradle jacoco 插件在 spring-gradle 项目中获得代码覆盖率。

build.gradle 包含以下内容

apply plugin: "jacoco"

    jacoco {
        toolVersion = "0.7.1.201405082137"
        reportsDir = file("$buildDir/customJacocoReportDir")
    }

    jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

然后我跑了

gradle test jacocoTestReport

在 build/reports 文件夹中仅生成文件 test.exec。

除此之外什么都没有发生。

如何获取 HTML 报告?

【问题讨论】:

    标签: gradle build.gradle


    【解决方案1】:

    你不必配置reportsDir/destinationFile

    因为 jacoco 有它们的默认值。

    build.gradle:

    plugins {
        id 'java'
        id 'jacoco'
    }
    
    jacocoTestReport {
        reports {
            xml.enabled true
            html.enabled true
            csv.enabled true
        }
    }
    
    repositories {
        jcenter()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }
    

    运行gradle test jacocoTestReport

    您可以在./build/reports/jacoco/test目录中找到测试报告。

    HTML 输出位于./build/reports/jacoco/test/html 目录中。

    【讨论】:

      【解决方案2】:

      以下帮助。它在 gradle-2.3-all.zip 的 samples/testing/jacaco 中来自https://gradle.org/releases/

      apply plugin: "java"
      
      apply plugin: "jacoco"
      
      jacoco {
          toolVersion = "0.7.1.201405082137"
          reportsDir = file("$buildDir/customJacocoReportDir")
      }
      
      repositories {
          mavenCentral()
      }
      
      dependencies {
          testCompile "junit:junit:4.+"
      }
      
      test {
          jacoco {
              append = false
              destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
              classDumpFile = file("$buildDir/jacoco/classpathdumps")
          }
      }
      
      
      jacocoTestReport {
          reports {
              xml.enabled false
              csv.enabled false
              html.destination "${buildDir}/jacocoHtml"
          }
      }
      

      【讨论】:

      • 语法 html.destination "${buildDir}/jacocoHtml" 在 Gradle 4.9 中已弃用,并将在 Gradle 5.0 中删除。请改用html.destination file("${buildDir}/jacocoHtml")
      • Gradle 6 不再支持 append 和 classDumpFile。
      【解决方案3】:
      subprojects {
          apply(plugin: 'org.jetbrains.kotlin.jvm')
      
          repositories {
              jcenter()
              mavenCentral()
         }
      }
      
      task codeCoverageReport(type: JacocoReport) {
      
          // Gather execution data from all subprojects
          executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
      
          // Add all relevant sourcesets from the subprojects
          subprojects.each {
              sourceSets it.sourceSets.main
          }
      
          reports {
              xml.enabled true
              html.enabled true
              csv.enabled false
          }
      }
      
      // always run the tests before generating the report
      codeCoverageReport.dependsOn {
          subprojects*.test
      }
      
      sonarqube {
          properties {
              property "sonar.projectKey", "your_project_key"
              property "sonar.verbose", true
              property "sonar.projectName", "Your project name"
              property "sonar.coverage.jacoco.xmlReportPaths", "${rootDir}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
          }
      }
      

      运行覆盖测试的命令:

      ./gradlew codeCoverageReport
      ./gradlew sonarqube -x test (test is excluded since already run and sonarqube by default executes test)
      

      如果未使用 sonarqube,则可以忽略第二个命令。

      使它起作用的两件事需要注意:

      1. 要使所有模块的源集可用,循环子项目和累积源集有效。 subprojects.sourceSets.main.allSource.srcDirs 不起作用。
      2. sonar.jacoco.reportPaths弃用。我们需要使用 sonar.coverage.jacoco.xmlReportPaths。在此处查看文档

      【讨论】:

        【解决方案4】:

        不幸的是,这些答案都不适合我。

        我遇到了类似的问题。
        不同之处在于不生成 exec 文件。
        正因为如此, 我发现 jacocoTestReport 只是“skipped”。

        我通过添加修复它

        apply plugin: 'jacoco'
        
        test {
          useJUnitPlatform()
          finalizedBy jacocoTestReport // report is always generated after tests run
        }
        
        jacocoTestReport {
            ...
            ...
            ...
            ...
        }
        

        那是因为我使用 Junit5 和 spring boot 2.X

        【讨论】:

          猜你喜欢
          • 2017-10-07
          • 1970-01-01
          • 2017-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-04
          • 2022-08-03
          相关资源
          最近更新 更多