gradle jacocoTestReport不工作?

时间:2015-03-17 11:00:37

标签: gradle build.gradle

我尝试使用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报告?

3 个答案:

答案 0 :(得分:11)

以下帮助。它的样品/测试/ jacaco of gradle-2.3-all.zip

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

答案 1 :(得分:6)

您不必配置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 :(得分:0)

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)

如果不使用声纳法,则可以忽略第二个命令。

需要注意的两点使其起作用:

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