Skip to content

scoverage/gradle-scoverage

Repository files navigation

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:

  • scoverageVersion = <String> (default "2.1.1): The version of the scoverage scalac plugin. This (gradle) plugin should be compatible with all 1+ versions.

  • scoverageScalaVersion = <String> (default detected): The scala version of the scoverage scalac plugin. This overrides the version of the scala-library compile dependency (if the dependency is configured).

  • coverageOutputCobertura = <boolean> (default true): Enables/disables cobertura.xml file generation (for both aggregated and non-aggregated reports).

  • coverageOutputXML = <boolean> (default true): Enables/disables scoverage XML output (for both aggregated and non-aggregated reports).

  • coverageOutputHTML = <boolean> (default true): Enables/disables scoverage HTML output (for both aggregated and non-aggregated reports).

  • coverageDebug = <boolean> (default false): Enables/disables scoverage debug output (for both aggregated and non-aggregated reports).

  • minimumRate = <double> (default 0.75): The minimum amount of coverage in decimal proportion (1.0 == 100%) required for the validation to pass (otherwise checkScoverage will fail the build).

  • coverageType = <CoverageType.Statement | CoverageType.Branch | CoverageType.Line> (default CoverageType.Statement): The type of coverage validated by the checkScoverage task. For more information on the different types, please refer to the documentation of the scalac plugin (https://github.com/scoverage/scalac-scoverage-plugin).

  • excludedFiles = <files> (default not set): Comma separated list of regexes for files to exclude from coverage.

  • excludedPackages = <packages, classes and modules> (default not set): Comma separated list of regexes for packages, classes and modules to exclude from coverage.

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

  • Requires scoverage 2.0
  • Adds support for Scala 3
  • Drops support for disabling normal compilation

Migration to 7.x

  • Running without normal compilation is now made with -PscoverageCompileOnly instead of -x compileScala.

Migration to 5.x

  • Requires scoverage 1.4.2 or higher (and uses this version by default)
  • Adds support for Scala 2.13
  • Drops support for Scala 2.11

Migration to 4.x

  • Requires scoverage 1.4.1 or higher (and uses this version by default)
  • Requires application of the plugin to appropriate subprojects. A multi-module project might apply it to all.
plugins {
    id 'org.scoverage' version '4.0.0'
}
subprojects {
    apply plugin: 'org.scoverage'
}

Migration to 3.x

  • No more testScoverage task; instead, test will run coverage whenever the build is invoked with any of the scoverage tasks.

  • No more need to declare scalac dependencies:

// 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'
}
  • All configurations are configured in scoverage block. For instance:
// do this
scoverage {
    minimumRate = 0.5
}

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