简体   繁体   中英

Display test coverage using jacoco in gradle

I am using a gradle file to build my project. In this file I am using jacoco to produce a test report. What I want to do is modify the build file so that it displays a message if my test coverage isn't 100%. Or at least just display the test coverage. Is this possible?

Here is my 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:

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

Then after ./gradlew jacocoTestReport , it shows:

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.
Full disclosure: I am the author of this little plugin.

At the moment this is not supported by the gradle jacoco plugin. You can vote for this issue at https://issues.gradle.org/browse/GRADLE-2783 and https://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. JUnit, JaCoCo and Cobertura reports are supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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