0
votes

I have a Gradle project configured with jacoco plugin to report the test code coverage. My unit tests are written in Spock framework.

Though the Jacoco plugin generates the HTML report, it reports the code coverage as 0% on all classes.

I googled a lot and couldn't find what I'm missing. Has anyone got the Spock code coverage working with Gradle + Jacoco?

apply plugin: "jacoco"
apply plugin: "groovy"

sourceSets {
    main {
        java { srcDirs = ['src/main/java'] }
        groovy {srcDirs = ['src/main/groovy'] }
        resources { srcDir 'src/main/resources' }
    }

    test {
        java { srcDirs = ['src/test/java'] }
        groovy { srcDirs = ['src/test/groovy'] }
        resources { srcDir 'src/test/resources' }
    }
}

test {
    jvmArgs '-Xms64m', '-Xmx2G', '-XX:MaxPermSize=128m'
}

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

dependencies {
    testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
    testCompile "org.spockframework:spock-spring:0.7-groovy-2.0"
}
1
Getting code coverage to work with Spock is no different to getting it to work with JUnit. Adding Groovy source dirs to sourceSets.main.java and sourceSets.test.java doesn't seem right. Also the doFirst doesn't seem right. Things like classDumpFile shouldn't have to be configured manually (unless you aren't happy with the defaults). Perhaps start from one of the JaCoCo sample builds in the full Gradle distribution.Peter Niederwieser
Thanks @PeterNiederwieser. I fixed the sourceSets.main.java and sourceSets.test.java, and removed classDumpFile. However I am not clear what is wrong with doFirst{}. Any clues?MFIhsan
There is no reason to wrap configuration of classDirectories with doFirst. I wasn't talking about classDumpFile in particular. Just saying that the more defaults are changed, the higher the chance of going wrong somewhere. Again, check the samples in the full Gradle distribution.Peter Niederwieser
I have removed the configurations overriding defaults. I have a plain-vanilla configuration now. Still the coverage is reported as 0%. I downloaded the latest full Gradle distribution looking for examples, but none of the samples have Jacoco configured in them.MFIhsan
I take my above comment back on Gradle sample. In fact there is a example under /samples/testing/jacoco. I have the exact same configuration in my project. The only difference is, my sources are in Java, tests are in Spock. I'm totally out of clues here.MFIhsan

1 Answers

6
votes

Suggestion from @PeterNiederwieser worked perfectly. Here is the final result:

apply plugin: "groovy"
apply plugin: "jacoco"

repositories { mavenCentral() }

dependencies {
  compile "org.codehaus.groovy:groovy-all:2.2.2"
  testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
}

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