how to prevent jacoco instrumenting production code?
Asked Answered
A

1

5

i use jacoco plugin for gradle:

apply plugin: 'kotlin'

jacoco {
    toolVersion = "0.7.9"
}
jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled false
        csv.enabled false
    }
}

and then i want to build a package for production

./gradlew build jacocoTestReport

the question is: will the generated package be instrumented by jacoco? if yes, how can build package NOT instrumented = ready for production? and having code coverage run? do i have to run build twice? is it impossible to build code once (sign it) and then test it, measure coverage etc and if all checks passes, deploy it?

Aweinspiring answered 15/3, 2018 at 10:1 Comment(3)
Why do you run code coverage during a production build anyway?Dominicdominica
to check if it's a shippable buildAweinspiring
IMHO this is usually done with the regular build job and not a release build. Code coverage is about quality and not functionality.Dominicdominica
L
6

JaCoCo provides two ways of performing instrumentation:

The difference is that in first case instrumentation happens in memory during execution and so no class or jar files will be changed on disk - quoting the second link:

One of the main benefits of JaCoCo is the Java agent, which instruments classes on-the-fly. This simplifies code coverage analysis a lot as no pre-instrumentation and classpath tweaking is required.

So one of the simplifications that Java agent brings - is exactly that you don't need to worry about packaging or multiple builds. This is IMO one of the advantages of JaCoCo over other coverage tools for Java such as Cobertura and Clover.

And this is one of the reasons why it is highly recommended to use on-the-fly instrumentation - quoting http://www.jacoco.org/jacoco/trunk/doc/cli.html :

the preferred way for code coverage analysis with JaCoCo is on-the-fly instrumentation with the JaCoCo agent. Offline instrumentation has several drawbacks and should only be used if a specific scenario explicitly requires this mode.

One of such specific scenarios - is execution of tests on Android, because there is no way to use Java agent on it. So AFAIK Android Plugin for Gradle, when instructed to measure coverage using JaCoCo, uses offline instrumentation and therefore requires two types of build - with coverage and without for release.

On the other hand JaCoCo Gradle Plugin, which integrates JaCoCo into Gradle for Java projects, AFAIK as of today provides ability to perform only on-the-fly instrumentation and not offline.

Loraleeloralie answered 16/3, 2018 at 17:50 Comment(2)
"requires two types of build - with coverage and without for release." -> Any link on how to configure that exactly?Heeled
@WimDeblauwe just in few clicks from link on Android Plugin for Gradle you can find google.github.io/android-gradle-dsl/current/… "Whether test coverage is enabled for this build type."Loraleeloralie

© 2022 - 2024 — McMap. All rights reserved.