I am attempting to generate instrumentation test coverage reports in Gradle to send to SonarQube. As this is a Kotlin codebase, I need to use Jacoco version 0.8.3+ to generate the reports to avoid the issue outlined here.
The custom task I created for generating unit test coverage reports works fine with the specified Jacoco version. But instrumented test coverage reports generated with the ./gradlew createDebugCoverageReport still generates a report with Jacoco version 0.7.9.
Is there a way to make ./gradlew createDebugCoverageReport or some other way to create an instrumentation test coverage report with a newer version of Jacoco plugin 0.8.3+?
Relevant parts of the top level build.gradle file:
buildscript {
dependencies {
classpath "org.jacoco:org.jacoco.core:0.8.5"
}
}
Relevant parts of the module level build.gradle file:
apply plugin:"com.android.library"
apply plugin: "jacoco"
android {
buildTypes {
debug {
testCoverageEnabled = true
}
}
}
jacoco {
toolVersion = "0.8.5"
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
group "Reporting"
description "Generate Jacoco coverage reports from unit tests."
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter =
['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*',
'android/**/*.*']
def javaDebugTree =
fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def kotlinDebugTree =
fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories.from.addAll(files([mainSrc]))
classDirectories.from.addAll(files([javaDebugTree, kotlinDebugTree]))
executionData.from.addAll(fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code-coverage/connected/*coverage.ec"
]))
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
Running the tasks sequentially test connectedDebugAndroidTest jacocoTestReport generates two sets of jacoco reports. The report from the connected tests with the older jacoco version, and the report for the unit tests with the correct jacoco version.
I was using a tool for mobile testing that produces coverage reports on a file called coverage.ec (using EMMA). The file is not readable but I would like to know how to access the info it contains. I just need to read the coverage percentage in general.
I was on a very similar situation. I had a coverage.ec file and I had no idea how to convert it into a readable report. This article was very helpful for me.
There is a lot more information there than you may need, but for me the important part was adding this to build.gradle:
apply plugin: 'jacoco'
def coverageSourceDirs = [
'src/main/java',
'src/debug/java']
task jacocoTestReport(type : JacocoReport, dependsOn : 'testDebugUnitTest') {
group = 'Reporting'
description = 'Generate JaCoCo coverage reports'
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir : 'build/intermediates/classes/debug',
excludes : [
'**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*RealmProxy.*',
'**/*ColumnInfo.*',
'**/*RealmModule*.*',
'**/AutoValue_*.*',
'**/Dagger*.*',
'**/*Module_Provide*Factory.*',
'**/*_Factory.*',
'**/*_MembersInjector.*',
'**/*_LifecycleAdapter.*'
]
)
sourceDirectories = files(coverageSourceDirs)
executionData = fileTree(
dir : "$buildDir",
include : [ 'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec' ]
)
doFirst {
files('build/intermediates/classes/debug').getFiles().each { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}}
From here, I placed my coverage.ec file in my app/build/outputs/code-coverage/connected/coverage.ec.
After that, I ran the jacocoTestReport gradle task and it generates a merged report for my unit tests and the reports from the coverage.ec file located in app/build/reports/jacoco/jacocoTestReport/html/index.html
Hope this helps!
This question already has answers here:
JaCoCo doesn't work with Robolectric tests
(6 answers)
Closed 5 years ago.
On my android project jacoco doesn't include the robolectic tests. I can get the android espresso and junit test coverage with jacoco without any issues.
I did see other questions about this issue and all the answers is to upgrade jacoco version. I'm using the latest jacoco version 0.7.9
This is my project main build.gradle
buildscript {
dependencies {
classpath 'org.jacoco:org.jacoco.core:0.7.9'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6-rc1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
App module build gradle.
apply plugin: 'jacoco'
android {
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
includeAndroidResources = true
}
}
}
I solve that problem with creating a separate task for jacoco in gradle.
First off all you need to add jacoco plugin.
apply plugin: "jacoco"
I didn't add any dependencies as you do on the code snippet above. Just add plugin.
Then add testCoverageEnabled true param to buildTypes section.
buildTypes {
debug {
testCoverageEnabled true
}
}
In this example it is in just for debug, but I believe if you add it for release it should also work.
Lastly add jacoco task like below;
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/jacoco/testDebug.exec")
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
Using that task you should be able to create coverage reports. It will export html formatted coverage report in build folder.
For more information you can look at this tutorial.
I am trying to use the code coverage tools of Jacoco on a Jenkins server for my Android library.
The builds are successful, and the code coverage stats are shown in the report :
However, when I want the details of which lines are covered or not, the page is totally empty:
As this is the thing I need the most, this is pretty annoying...
Here is the part of the gradle file I have for Jacoco:
apply plugin: 'jacoco'
jacoco {
// Use this version for upper ones are broken (with Gradle)
// https://github.com/jacoco/jacoco/issues/288
toolVersion = "0.7.1.201405082137"
}
// Edit covered scope if needed
def coverageSourceDirs = [
'../s3papiandroidclient/src'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "test") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../s3papiandroidclient/build/intermediates/classes',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testUnitTestDebug.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
jacocoTestReport.doFirst{
delete fileTree (dir: "../s3papiandroidclient/build/intermediates/classes", include: "**/*Test.class")
}
Anyone encountered something similar ?
Regards,
Gyoo.
I recently started integrating android-gradle-plugin 1.1.0 in one of my projects. The project uses robolectric 2.4 to run unit tests.
It's a multi module project with very complex dependencies (Some modules depend on other modules). Something like that:
--> application-module (dependsOn: module1, module2, module-core)
--> module1 (dependsOn: module-core)
--> module2 (dependsOn: module-core)
--> module-core (dependsOn: module3, module4)
--> module3 (library dependencies)
--> module4 (library dependencies)
For a more cleared picture please see jacoco-example project.
I tried to integrate JaCoCo to generate reports for the unit tests, but it seems to me that it runs only androidTests which are basically instrumentation tests.
After some google'ing I've come across a few projects on GitHub and other articles, but they mainly are focused on previous versions of the android-gradle-plugin or are using other third party plugins like android-unit-test for example here.
May be I've lost my ability to google. But can somebody point me in a direction where I can find some documentations regarding the new stuff in android gradle plugin and how to run the jacoco task only for unit tests?
UPDATE
Adopted the script from nenick's example:
apply plugin: "jacoco"
configurations {
jacocoReport
}
task jacocoReport(dependsOn: 'testDebug') << {
ant {
taskdef(name:'jacocoreport',
classname: 'org.jacoco.ant.ReportTask',
classpath: configurations.jacocoReport.asPath)
mkdir dir: "${buildDir}/test-coverage-report"
mkdir dir: "${buildDir}/reports/jacoco/test/"
jacocoreport {
executiondata = files("${buildDir}/jacoco/testDebug.exec")
structure(name: "${rootProject.name}") {
classfiles {
fileset (dir: "${buildDir}/intermediates/classes/debug") {
//exclude(name: '**/*_*.class')
exclude(name: '**/R.class')
exclude(name: '**/R$*.class')
exclude(name: '**/BuildConfig.class')
}
}
sourcefiles {
fileset dir: "src/main/java"
fileset dir: "${buildDir}/generated/source/buildConfig/debug"
fileset dir: "${buildDir}/generated/source/r/debug"
}
}
xml destfile: "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
html destdir: "${buildDir}/test-coverage-report/"
}
}
}
dependencies {
jacocoReport 'org.jacoco:org.jacoco.ant:0.7.2.201409121644'
}
After that the ./gradlew jacocoReport executes and generates the report, but it shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.
UPDATE_2
Tried out this example. Adding the next task to one of my gradle build files:
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: "${buildDir}/intermediates/classes/debug",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
sourceDirectories = files("${buildDir.parent}/src/main/java")
additionalSourceDirs = files([
"${buildDir}/generated/source/buildConfig/debug",
"${buildDir}/generated/source/r/debug"
])
executionData = files("${buildDir}/jacoco/testDebug.exec")
reports {
xml.enabled = true
html.enabled = true
}
}
Same issue, the reports are generated, but the code coverage is still zero.
UPDATE_3
It seams that the task from UPDATE_2 worked but only for the module with apply plugin: 'com.android.application' (The reports a generated correctly). But for modules that are android libraries (apply plugin: 'com.android.library') the reports show zero coverage, although the modules contain more tests then the application module.
UPDATE_4
Created a simple example project that demonstrates my issue. Currently if you run ./gradlew jacocoReport the report is generated, but no test coverage is displayed for the module projects. See this link
Short note: When the tests were AndroidUnitTests (whiteout JUnit 4 and Robolectric) JaCoCo reports showed coverage for all the modules.
Any ideas?
After the hassle, I decided to create an open source Gradle plugin for that.
Root build.gradle
buildscript {
repositories {
mavenCentral() // optional if you have this one already
}
dependencies {
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.16.0'
}
}
apply plugin: 'com.vanniktech.android.junit.jacoco'
Then simply execute
./gradlew jacocoTestReportDebug
It'll run the JUnit tests in Debug Mode and then give you the Jacoco output in XML and HTML form in the corresponding build directory.
It also supports flavors. Having 2 flavors red and blue those tasks would be created
jacocoTestReportRedDebug
jacocoTestReportBlueDebug
jacocoTestReportRedRelease
jacocoTestReportBlueRelease
After some additional search I've stumbled upon this project
I had to make some modifications so that there solution can work for my type of project, but now the test coverage reports are generated properly.
I've pushed the adopted changes to my example github repo in case someone will have a similar problem in the future.
Warning: This is a hack! Using your configuration above, I put together a hack to switch the android plugin between application and library depending on the build tasks chosen. This works well for me because I don't end up committing code with the application mode set.
// dynamically change the android plugin to application if we are running unit tests or test reports.
project.ext.androidPlugin = 'com.android.library'
for (String taskName : project.gradle.startParameter.taskNames) {
if (taskName.contains('UnitTest') || taskName.contains('jacocoTestReport')) {
project.ext.androidPlugin = 'com.android.application'
break
}
}
logger.lifecycle("Setting android pluging to ${project.ext.androidPlugin}")
apply plugin: project.ext.androidPlugin
...
apply plugin: 'jacoco'
configurations {
jacocoReport
}
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: "${buildDir}/intermediates/classes/debug",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
sourceDirectories = files("${buildDir.parent}/src/main/java")
additionalSourceDirs = files([
"${buildDir}/generated/source/buildConfig/debug",
"${buildDir}/generated/source/r/debug"
])
executionData = files("${buildDir}/jacoco/testDebug.exec")
reports {
xml.enabled = true
html.enabled = true
}
}
Let's hope the android tools team fixes this soon.
I setup my unit tests for gradle 1.2 using this blog post. Then I pieced together information I found here and elsewhere to add code coverage to independent modules instead of the whole project. In my library module build.gradle file, I added the following:
apply plugin: 'jacoco'
def jacocoExcludes = [
'com/mylibrary/excludedpackage/**'
]
android {
...
}
android.libraryVariants.all { variant ->
task("test${variant.name.capitalize()}WithCoverage", type: JacocoReport, dependsOn: "test${variant.name.capitalize()}") {
group = 'verification'
description = "Run unit test for the ${variant.name} build with Jacoco code coverage reports."
classDirectories = fileTree(
dir: variant.javaCompile.destinationDir,
excludes: rootProject.ext.jacocoExcludes.plus(jacocoExcludes)
)
sourceDirectories = files(variant.javaCompile.source)
executionData = files("${buildDir}/jacoco/test${variant.name.capitalize()}.exec")
reports {
xml.enabled true
xml.destination "${buildDir}/reports/jacoco/${variant.name}/${variant.name}.xml"
html.destination "${buildDir}/reports/jacoco/${variant.name}/html"
}
}
}
And in my project build.gradle file, I added common excludes:
ext.jacocoExcludes = [
'android/**',
'**/*$$*',
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Service.*'
]
Also, it looks like code coverage for unit tests may be coming built in in the future Issue 144664
I was finally able to see my code coverage of JUnit tests with Android Studio 1.1.
jacoco.gradle
apply plugin: 'jacoco'
jacoco {
toolVersion "0.7.1.201405082137"
}
def coverageSourceDirs = [
"$projectDir/src/main/java",
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
]
)
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/jacoco/testDebug.exec")
// Bit hacky but fixes https://code.google.com/p/android/issues/detail?id=69174.
// We iterate through the compiled .class tree and rename $$ to $.
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
and then within the build.gradle file of the module (I put it between android and dependencies):
apply from: '../jacoco.gradle'
Also in the defaultConfig block of android. I've added this (don't know if it is necessary, but I've got this from this blog):
android {
defaultConfig {
testHandleProfiling true
testFunctionalTest true
}
}
Enjoy.
You can try to use this Gradle plugin:
https://github.com/arturdm/jacoco-android-gradle-plugin
Basically, all you need to do is apply it like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.1'
}
}
apply plugin: 'com.android.library' // or 'com.android.application'
apply plugin: 'jacoco-android'
As a result you should get a JacocoReport task for each variant. Run the command below to generate code coverage reports for all of them.
$ ./gradlew jacocoTestReport
I resolve issues with JaCoCo and make it work with latest gradle android plugin 1.1.3
Project with latest gradle scripts: https://github.com/OleksandrKucherenko/meter
References:
How to attach own implementation instead of Mocks in Android Studio Unit Tests?
https://plus.google.com/117981280628062796190/posts/8jWV22mnqUB
Small hint for everyone who try to use JaCoCo coverage in android builds... unexpected finding!!!
https://plus.google.com/117981280628062796190/posts/RreU44qmeuP
JaCoCo XML/HTML Report for Unit Tests https://plus.google.com/u/0/+OleksandrKucherenko/posts/6vNWkkLed3b
I was facing exactly the same problem like you. Today I did completely removed android studio, android sdk, gradle. Then reinstall everything. After that, I just added inside the app build.gradle.
debug {
testCoverageEnabled true
}
Then I run ./gradlew connectedChec. Everything is working perfectly. Android studio default Jacoco working fine for me. I think it is also possible to create a jacocoTestReport Task and then create code coverage.I don't know why gradle and android studio was not working previously.
Please create an example and I can take a look. I guess it's some missing path configuration.
include all coverage files (*.exec)
add all your source paths (module/src/main/java)
add all class paths (module/build/intermediates/classes/debug)
here two examples how it could be look
https://github.com/nenick/AndroidAppDevelopment/blob/master/Scripts/jacoco-support-app-module.gradle
https://github.com/nenick/AndroidAppDevelopment/blob/master/Scripts/jacoco-coveralls-support.gradle