Trying to get code coverage on my Robolectric tests on Android using Jacoco, but just refuses to recognize my Robolectric tests when reporting.
My jacoco.gradle file is as follows:
apply plugin: 'jacoco' jacoco { toolVersion = "0.7.6.201602180812" } project.afterEvaluate { android.applicationVariants.all { variant -> def name = variant.name def testTaskName = "test${name.capitalize()}UnitTest" tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") { group = "Reporting" description = "Generate Jacoco coverage reports for the ${name.capitalize()} build." classDirectories = fileTree( dir: "${project.buildDir}/intermediates/classes/${name}", excludes: ['**/R.class', '**/R$*.class', '**/*$ViewInjector*.*', '**/*$ViewBinder*.*', '**/BuildConfig.*', '**/Manifest*.*'] ) sourceDirectories = files(['src/main/java'].plus(android.sourceSets[name].java.srcDirs)) executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec") reports { xml.enabled = true html.enabled = true } } } }
With this setting, I can get coverage reports, but I get 0% coverage, even though I have Robolectric tests in "src / test / java".
If I add the following code to this file:
android { testOptions { unitTests.all { jacoco { includeNoLocationClasses = true } } } }
I get the following error when Gradle tries to sync:
Error: no such property: includeNoLocationClasses for class: org.gradle.testing.jacoco.plugins.JacocoTaskExtension_Decorated
I know that I need to have Gradle 2.13 to use this includeNoLocationClasses value, so in graddle-wrapper.properties I have the following:
I am sure that I am running Gradle 2.13 with the Android plugin version 1.5
In my Gradle application files, I have the following:
//... apply from: 'jacoco.gradle' //.. testOptions { unitTests.returnDefaultValues = true } //... debug { testCoverageEnabled true }
And the command that I use to start coverage is:
./gradlew createDebugCoverageReport
So, I wonder why I get the includeNoLocationClasses error despite using the correct version of Gradle? And beyond that, maybe I'm doing something wrong when Jacoco does not collect Robolectric tests in src / test.java?