Home

Awesome

Java CI with Gradle

gradle-scoverage

A plugin to enable the use of Scoverage in a gradle Scala project.

Usage

You can find instructions on how to apply the plugin at http://plugins.gradle.org/plugin/org.scoverage

Available tasks

  1. reportScoverage: Produces XML and HTML reports for analysing test code coverage.

    The plugin automatically creates a report{Task name}Scoverage task for each test task in your Gradle build. The reportScoverage task will run all test tasks and you can use the individual tasks to run only the desired tests. For example, to run only the unit tests and no other test tasks (e.g., integration tests), you can run reportTestScoverage.

  2. aggregateScoverage: Aggregates coverage statistics in composite builds.

    When applied on a project with sub-projects, the plugin will create the aggregation task aggregateScoverage, which will first generate reports for each project individually (including the parent project), and will then generate an aggregated result based on these reports.

    The plugin must be applied on a sub-project for it to be included in the aggregated; applying the plugin on a project does not automatically apply it on sub-projects.

    The aggregated report will override the parent-project specific report (parent-project/build/reports/scoverage).

    One can still use reportScoverage in order to generate a report without aggregation.

  3. checkScoverage: Validates coverage status according to generated reports (aggregated or not).

    gradle checkScoverage will automatically invoke reportScoverage but it won't generate aggregated reports. In order to check coverage of aggregated reports one should use gradle checkScoverage aggregateScoverage.

Note: The plugin is not compatible with composite builds. For more information, see the relevant issue.

Configuration

The plugin exposes multiple options that can be configured by setting them in an scoverage block within the project's build script. These options are as follows:

Multiple check tasks

It is possible to configure multiple checks; for instance, one check for a statement rate and another for a branch rate:

scoverage {
    check {
        minimumRate = 0.5
        coverageType = CoverageType.Statement
    }
    check {
        minimumRate = 0.8
        coverageType = CoverageType.Branch
    }
}

Note that you cannot mix multiple-checks syntax with plain check configuration:

// ok
scoverage {
    check {
        minimumRate = 0.5
        coverageType = CoverageType.Statement
    }
}

// ok
scoverage {
    minimumRate = 0.2
}

// NOT ok
scoverage {
    minimumRate = 0.2
    check {
        minimumRate = 0.5
        coverageType = CoverageType.Statement
    }
}

In case you use the Kotlin DSL, the following snippet can give you pointers for configuring Scoverage:

scoverage {
	minimumRate.set(BigDecimal("0.80"))
	excludedPackages.set(listOf<String>("com.example.scala.demo"))
}

Compatibility with Consistent Versions Plugin

In order for the plugin to work alongside Palantir's consistent versions plugin, the Scala version must be manually configured (via scoverageScalaVersion); otherwise, the plugin will attempt to resolve the compilation classpath, which is prohibited by the versions plugin.

Migration to 8.x

Migration to 7.x

Migration to 5.x

Migration to 4.x

plugins {
    id 'org.scoverage' version '4.0.0'
}
subprojects {
    apply plugin: 'org.scoverage'
}

Migration to 3.x

// can safely delete this from build scripts
dependencies {
    scoverage group: 'org.scoverage', name: 'scalac-scoverage-plugin_2.12', version: '1.3.1'
    scoverage group: 'org.scoverage', name: 'scalac-scoverage-runtime_2.12', version: '1.3.1'
}
// do this
scoverage {
    minimumRate = 0.5
}

// instead of this
checkScoverage {
    minimumRate = 0.5
}
// can safely delete this from build scripts
task aggregateScoverage(type: org.scoverage.ScoverageAggregate)
checkScoverage {
     reportDir = file("$buildDir/scoverage-aggregate")
}