简体   繁体   English

在 gradle 中使用 jacoco 显示测试覆盖率

[英]Display test coverage using jacoco in gradle

I am using a gradle file to build my project.我正在使用 gradle 文件来构建我的项目。 In this file I am using jacoco to produce a test report.在此文件中,我使用 jacoco 生成测试报告。 What I want to do is modify the build file so that it displays a message if my test coverage isn't 100%.我想要做的是修改构建文件,以便在我的测试覆盖率不是 100% 时显示一条消息。 Or at least just display the test coverage.或者至少只显示测试覆盖率。 Is this possible?这可能吗?

Here is my build.gradle:这是我的 build.gradle:

apply plugin: 'java'
apply plugin: 'jacoco'

sourceSets.main.java.srcDirs = ['src']
sourceSets.test.java.srcDirs = ['test']

dependencies {
    testCompile group: 'junit', name: 'junit', version: "4.+"
}

repositories {
    mavenCentral()
}

jacocoTestReport {
    doFirst {
      classDirectories = files('build/classes/main/draw')
    }
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "build/reports/coverageHtml"
    }
}

task(runui, dependsOn: jar, type: JavaExec) {

    main = 'gui.Main'
    classpath = sourceSets.main.runtimeClasspath
}

defaultTasks 'clean', 'test', 'jacocoTestReport', 'runui'

There is a very simple Gradle plugin called gradle-jacoco-log that simply logs the Jacoco coverage data:有一个非常简单的 Gradle 插件,叫做gradle-jacoco-log ,它可以简单地记录 Jacoco 覆盖率数据:

plugins {
    id 'org.barfuin.gradle.jacocolog' version '1.0.1'
}

Then after ./gradlew jacocoTestReport , it shows:然后在./gradlew jacocoTestReport ,它显示:

Test Coverage:
    - Class Coverage: 100%
    - Method Coverage: 100%
    - Branch Coverage: 81.2%
    - Line Coverage: 97.8%
    - Instruction Coverage: 95.3%
    - Complexity Coverage: 90.2%

There are also some options to customize what is logged.还有一些选项可以自定义记录的内容。

The other topic of enforcing a certain test coverage is covered by Gradle these days.这些天,Gradle 涵盖了强制执行某个测试覆盖率的另一个主题。
Full disclosure: I am the author of this little plugin.完全披露:我是这个小插件的作者。

At the moment this is not supported by the gradle jacoco plugin.目前 gradle jacoco 插件不支持此功能。 You can vote for this issue at https://issues.gradle.org/browse/GRADLE-2783 and https://issues.gradle.org/browse/GRADLE-2854 .您可以在https://issues.gradle.org/browse/GRADLE-2783https://issues.gradle.org/browse/GRADLE-2854 上为此问题投票。 As a workaround you could possibly parse the output file in a custom task.作为一种解决方法,您可以在自定义任务中解析输出文件。

You can use Gradle plugin gradle-console-reporter to report various kinds of summaries to console.您可以使用 Gradle 插件gradle-console-reporter控制台报告各种摘要。 JUnit, JaCoCo and Cobertura reports are supported.支持 JUnit、JaCoCo 和 Cobertura 报告。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM