Android - Jacoco code coverage ignores Robolectric tests

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:

 #Wed Apr 10 15:27:10 PDT 2013 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions-snapshots/gradle-2.13-20160228000026+0000-all.zip 

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?

+7
java android testing robolectric jacoco
source share
1 answer

I do not see you fully build.gradle , but to have this flag you need:

  • Use gradle 2.13+
  • Use jacoco 0.7.6.201602180812

Are you sure you are using gradle the correct version. So, I think the problem is only using the wrong jacoco .

Mention of jacoco {toolVersion = "0.7.6.201602180812"} does not affect the DSL gradle . You should add a new jacoco plugin:

 buildscript { repositories { jcenter() } dependencies { classpath 'org.jacoco:org.jacoco.core:...' } } 

And you should apply the plugin that you are already doing:

 apply from: 'jacoco' 

After this configuration, you no longer need jacoco {toolVersion = "..."} .

Note: consider upgrading to a newer one and Android gradle , 2.2.x already stable. jacoco also has a newer version 0.7.7.201606060606

One more note: if you see the original problem in Android Studio, make sure you use the default shell and make sure you point the shell to gradle 2.13

+6
source share

All Articles