Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 532
ThomYorkkke Avatar asked Nov 21 '14 03:11

ThomYorkkke


2 Answers

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.

like image 199
barfuin Avatar answered Nov 14 '22 13:11

barfuin


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.

like image 35
Rene Groeschke Avatar answered Nov 14 '22 15:11

Rene Groeschke