Skip to content
Thad Guidry edited this page Aug 24, 2017 · 13 revisions

Cobertura Plugin

The Cobertura plugin adds cobertura code coverage targets to a project. See announcements and changes here.

Usage

To use, add the following to your build.gradle file:

Gradle 1.0-milestone-7 and later:

buildscript {
    apply from: 'https://github.com/valkolovos/gradle_cobertura/raw/master/repo/gradle_cobertura/gradle_cobertura/1.0/coberturainit.gradle'
}

Gradle 1.0-milestone-6 and earlier:
buildscript {
    apply from: 'https://github.com/valkolovos/gradle_cobertura/raw/master/repo/gradle_cobertura/gradle_cobertura/1.0-rc4/coberturainit.gradle'
}

The code will only be instrumented if a GenerateCoverageReportTask is in the task graph.

Tasks

The Cobertura plugin adds the following tasks to a project:

  • cobertura – Executes tests and generates a cobertura code coverage report in the directory specified by the convention’s reportDir property
    • depends on: cleanTest, test
    • type: com.orbitz.gradle.cobertura.tasks.GenerateCoverageReportTask

Customizing

You can customize the behavior of the Cobertura plugin by using a closure to configure the convention properties

cobertura {
}

Convention Properties

  • coverageDirs (type: List) – Directories under the base directory containing classes to be instrumented.
    • default value is [ project.sourceSets.main.classesDir.path ]
  • coverageDatafile (type: File) – Path to data file to use for Cobertura.
    • default value is project.buildDir.path/cobertura/cobertura.ser
  • coverageReportDir (type: String) -Path to report directory for coverage report.
    • default value is project.reportsDir.path/cobertura
  • coverageFormat (type: String) – Format of cobertura report.
    • default value is html
  • coverageSourceDirs (type: Set) – Directories of source files to use.
    • default value is project.sourceSets.main.java.srcDirs
  • coverageIncludes (type: List) – List of regular expressions to specify which class files to include in coverage report
    • default value is an empty set
  • coverageExcludes (type: List) – List of regular expressions to specify which class files to exclude from coverage report
    • default value is an empty set

These properties are provided by a org.gradle.api.plugins.CoberturaConvention object

GenerateCoverageReportTask

You can add custom instances of this task to do generate additional coverage reports. For example, you might want to have an XML format of the coverage report generated. To do that, you would add another task of type GenerateCoverageReportTask like this:

task generateXmlCoverage(type: com.orbitz.gradle.cobertura.tasks.GenerateCoverageReportTask, dependsOn: 'cobertura') {
    format = 'xml'
    coverageExcludes = [ '.*org\\.foo\\..*' ]
}

Gradle usage and Jenkins

You can create a custom cobertura.gradle plugin file and then apply this in your existing build.gradle file like so:

cobertura.gradle file

buildscript {
    repositories {
      mavenCentral()
	dependencies.classpath 	'net.saliman:gradle-cobertura-plugin:2.3.2',
				'net.sourceforge.cobertura:cobertura:2.1.1',
				'org.apache.commons:commons-lang3:3.2',
				'oro:oro:2.0.8',
				'org.ow2.asm:asm:5.0.1',
				'org.ow2.asm:asm-analysis:5.0.1',
				'org.ow2.asm:asm-commons:5.0.1',
				'org.ow2.asm:asm-tree:5.0.1',
				'org.ow2.asm:asm-util:5.0.1'
    }
}
 
// We use the type of the plugin instead of the id.
// This is the class that defines the plugin. We can leave off 
// .class, because Gradle uses Groovy.
apply plugin: net.saliman.gradle.plugin.cobertura.CoberturaPlugin

//check if the environment is JENKINS or LOCAL
def jenkinsHome = System.getenv('JENKINS_HOME')

//default build dir of local gradle
def jobpath = new File(buildDir.toString() + "/reports/cobertura")
if(jenkinsHome)
    jobpath = new File(System.getenv('JENKINS_HOME') + "/jobs/" + System.getenv('JOB_NAME') + "/builds/reports/cobertura")
mkdir jobpath

cobertura {
    coverageFormats = ['html', 'xml']				
    coverageIgnores = ['org.slf4j.Logger.*']	//only an example to skip instrumentation
    coverageReportDir = jobpath
}

test.finalizedBy(project.tasks.cobertura)
logger.info('DONE Configuring Cobertura Plugin')

build.gradle file

apply plugin: 'java'
apply plugin: 'eclipse'
apply from: 'cobertura.gradle'

dependencies {
    testRuntime "org.slf4j:slf4j-api:1.7.10"
}

task doStuff {
  // STUFF...
}

/*
// This is just an example of how to customize the Cobertura coverage reporting
// to exclude certain classes from being inspected and reported.
cobertura {
   coverageExcludes =
  ['.*com.example.gateway.cache.*',
   '.*com.example.gateway.common.*',
   '.*com.example.gateway.model.*',
   '.*com.example.gateway.dao.ConfigParamDao.*'
  ]
}
*/

Troubleshooting

1. Instrumentation errors might appear where getters/setters are used and so you might need to set this parameter like so, or remove the whole line:

coverageIgnoreTrivial = false

2. If you get all failing tests in builds then you will need to add the SLF4J library into your build.gradle like so:

dependencies {
    testRuntime "org.slf4j:slf4j-api:1.7.10"
}

If you add SLF4J in the cobertura.gradle file in the dependencies.classpath, it does not have any impact and the tests will continue to fail, so add it instead as a testRuntime dependency in your build.gradle file.
Clone this wiki locally