Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle skip JaCoCo during test

Tags:

gradle

jacoco

I have an issue in which test task is failing intermittently (I doubt it is, because Jacoco is triggering [generating reports] during test).

Is there a way to disable running of jacoco during test?

I can create a new task solely for generating coverage reports (I would like to know how that can be done too). There are many cases in which I don't really need a test coverage report while running test task.

like image 999
Suraj Muraleedharan Avatar asked Feb 28 '17 06:02

Suraj Muraleedharan


2 Answers

I found that if you do the apply plugin then the jacoco instrumentation takes place even if you have done the dependsOn.remove as noted in the accepted answer. You can tell the instrumentation is still occurring as a file called build/jacoco/test.exec is created even if the jacoco reports themselves are not created.

I had to extract the jacoco plugin apply to a separate .gradle file and conditionally include it like:

if (jacocoEnabled.toBoolean() )  {
  project.logger.lifecycle('applying jacoco build file')
  apply from: "jacoco.gradle"
}

Then my jacoco.gradle file looks like:

apply plugin: 'java'
apply plugin: 'jacoco'


test {
  jacoco {
    append = false
    destinationFile = file("$buildDir/jacoco/test.exec")
  }
}

  jacocoTestReport {
      reports {
          xml.enabled true
          xml.destination file("${buildDir}${jacocoXMLDestination}")
      }
  }

  test.finalizedBy jacocoTestReport

This took my build time from 4 minutes to 3 minutes - providing some savings.

like image 63
Fresh Codemonger Avatar answered Oct 04 '22 02:10

Fresh Codemonger


Edit


After reading the second answer and testing it myself I greatly suggest and appreciate the below blog post as it's true the instrumentation still happens even after disabling or removing the tasks.

https://poetengineer.postach.io/post/how-to-conditionally-enable-disable-jacoco-in-gradle-build


If it does runs on Jenkins, error 137 might be out of memory issue.

If it runs on Jenkins please try to extend the memory and check this link.

I'm getting OutOfMemoryError

Jacoco tasks:

jacocoTestReport    -   JacocoReport    Generates code coverage report for the test task.
jacocoTestCoverageVerification  -   JacocoCoverageVerification  Verifies code coverage metrics based on specified rules for the test task.

To find out which tasks have which dependencies you can do

gradle tasks --all

To exclude the task with cmd you can

gradle test -x taskToExclude

Programmatically you can use a task graph to exclude it

gradle.taskGraph.useFilter { task -> yourstuff}

or simply remove it from test task dependencies

test.dependsOn.remove("jacocoTestReport")
test.dependsOn.remove("jacocoTestCoverageVerification")

Additional resource worth checking: https://docs.gradle.org/current/userguide/jacoco_plugin.html

like image 38
LazerBanana Avatar answered Oct 04 '22 01:10

LazerBanana