41

I'm using Gradle 1.7 and Jacoco plugin. My project uses Java and Scala plugins.
When I run gradlew -i clean jacocoTestReport

Report is not created and I see in the log

:bl:jacocoTestReport (Thread[Daemon Thread 13,5,main] - start
:bl:jacocoTestReport
Skipping task ':bl:jacocoTestReport' as task onlyIf is false.
:bl:jacocoTestReport SKIPPED
:bl:jacocoTestReport (Thread[Daemon Thread 13,5,main]) - complete

What does it mean? Why report is not created?

JJD
  • 50,076
  • 60
  • 203
  • 339
Pavel Bernshtam
  • 4,232
  • 8
  • 38
  • 62

5 Answers5

42

The task will only run if coverage data is available. You can make sure of that by also running the test task.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • And make sure you have both "androidTest" and "test" folders with at least one test in one of them. At least one test in each folder will be better – display name May 19 '16 at 01:46
  • 6
    Note that your inclusion of the 'clean' task is significant as well. If you had previously run the 'test' task before you added the jacoco plugin, then although coverage data will not have been generated, the 'test' task will think it is UP-TO-DATE, and coverage data will not be generated even after adding 'jacoco'. You'd need to either use 'clean', or make some change to the codebase to force the 'test' task to run again and generate coverage data. It looks like the OP got this right, but for the benefit of others, this is what had me stumped for a while. – simonh Sep 23 '16 at 10:09
  • @simonh got a great point over here, make sure you clean the project before you run jacoco after you added this. – BenBen Oct 20 '17 at 18:20
  • so should this look like `$ ./gradlew clean test myJacocoTestCoverageVerification` ?? – jesses.co.tt Aug 27 '19 at 20:12
34

Add the following at a top level to your build.gradle:

test {
 finalizedBy jacocoTestReport
}

This means that at the end of the test task the jacocoTestReport task should be run. You will receive your coverage analysis after run the tests.

Fabian Hertwig
  • 1,093
  • 13
  • 27
  • 2
    Can you be a bit more precise and say where to add it ? – Laurent Meyer Oct 19 '15 at 14:13
  • Above will not work as in OP jacocoTesReport was already running but skipping report generation: ``` Skipping task ':bl:jacocoTestReport' as task onlyIf is false. :bl:jacocoTestReport SKIPPED ``` – Farrukh Najmi May 01 '17 at 11:46
  • 2
    the `test{}` block doesn't exist on Android gradle-based projects - would this be `unitTests.all {}` ?? – jesses.co.tt Aug 27 '19 at 20:14
14

None of the above worked for me. What worked for me was the following

Add to the top of my build.gradle:

apply plugin: 'jacoco' // code coverage reports

Add the following as a 'task':

// Generate code coverage reports ... run with jacoco
jacocoTestReport{
    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/reports/jacoco/html"
    }
    executionData = files('build/jacoco/test.exec')
}

Add the following to your gradle test task:

finalizedBy jacocoTestReport

Then I issued the following command:

gradle run test jacoco
TayTay
  • 6,882
  • 4
  • 44
  • 65
StylusEater
  • 3,769
  • 1
  • 15
  • 13
  • 1
    Great! I was outputing my exec data to a custom directory and couldn't understand why jacocoTestReport kept skipping. Turns out you need to specify the location of those binary (.exec) files via executionData – Pranav Apr 16 '19 at 22:09
  • * What went wrong: Task 'jacoco' is ambiguous in root project Cannot set the value of read-only property 'additionalSourceDirs' for task ':jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport. – john k Jul 23 '21 at 14:06
1

For me the issue was that the executionData.setFrom(executionSource) file path was wrong.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

For Spring 2.5 Users, who got stuck with it for hours -just like myself.
I was not having the exec file generated.
And because of that , I found that the jacocoTestReport was simply "skipped".

I got it fixed by adding :

apply plugin: 'jacoco'

test {
  useJUnitPlatform()
  finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
    ...
    ...
    ...
    ...
}

That's because I'm using Junit5 with spring boot 2.X
And as of today Junit5 is not by default in the test task,
Hope this helps.

M. Amer
  • 916
  • 10
  • 12