11

Project structure:

    src/main/java
    src/main/resources
    src/test/java

Gradle version : 2.2.1

Here is my build.gradle

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'jacoco'
    version = '1.0'
   sourceCompatibility = 1.7
   targetCompatibility = 1.7

 test {
    include 'src/test/java'
    finalizedBy jacocoTestReport

   }

  jacoco {
    toolVersion = "0.7.6.201602180812"

  }

 jacocoTestReport {
  group = "Reporting"
  description = "Generate Jacoco coverage reports after running tests."
  additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
  reports {
      xml.enabled false
      csv.enabled false
      html.destination "${buildDir}/reports/jacoco/html"
}
}

when I run gradle task as "test jacocoTestReport", I am getting the below results

  :compileJava UP-TO-DATE
  :processResources UP-TO-DATE
  :classes UP-TO-DATE
  :compileTestJava UP-TO-DATE
  :processTestResources UP-TO-DATE
  :testClasses UP-TO-DATE
  :test UP-TO-DATE
  :jacocoTestReport SKIPPED

can someone please suggest what should be added to execute jacoco test report.

Thanks.

Kishore
  • 317
  • 2
  • 4
  • 17

5 Answers5

8

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

Source - Running jacocoReport

Community
  • 1
  • 1
ericdemo07
  • 461
  • 8
  • 16
4

I was able to generate the code coverage results with the following set up.

 apply plugin: 'jacoco'
 jacocoTestReport {
   reports {
   xml.enabled false
   csv.enabled false
   html.destination "${buildDir}/jacocoHtml"
 }
}
Kishore
  • 317
  • 2
  • 4
  • 17
2

You can force it to run with:

jacocoTestReport { onlyIf = { true } }

This will probably give an error (there's a reason it didnt run in the first place), but the error should give more information.

john k
  • 6,268
  • 4
  • 55
  • 59
2

Unfortunately, none of these answers worked for me.
For Spring 2.5 Users, who got stuck with it for hours -just like myself.

I had a similar issue.
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 :

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

That's because I'm using Junit5 with spring boot 2.X - Gradle 7.1
And as of today Junit5 is not invoked by default in the test task.

M. Amer
  • 916
  • 10
  • 12
  • 1
    Finally! adding `useJUnitPlatform()` is what worked for me. without this the tests weren't even running I guess. – Sumit Jan 14 '22 at 02:06
0

Late at party, but none of the answers above solved for me. Instead, changing

dependencies {
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}

to

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}

worked like a charm (source).

enzo
  • 9,861
  • 3
  • 15
  • 38