1. Joachim Hofer
  2. cobertura4sbt

Wiki

Clone wiki

cobertura4sbt / Home

cobertura4sbt - Code Coverage via Cobertura from within SBT

This SBT plug-in enables you to measure your code coverage with the help of the great Cobertura tool.

However, since Cobertura is not really actively developed any more, I decided to stop working on this plugin for the time being, and instead started jacoco4sbt. Please ponder using jacoco4sbt instead of this plugin...

Getting cobertura4sbt

cobertura4sbt is hosted at the Scala-Tools.org Maven repository. This repository is automatically available from within SBT. This means that you just have to add cobertura4sbt as a plug-in to your project (see the next section).

If you want to go bleeding edge, you can also:

  hg clone http://bitbucket.org/jmhofer/cobertura4sbt
  cd cobertura4sbt
  sbt publish-local

Adding cobertura4sbt as a plug-in to your project

Just add the following line to your project/plugins/Plugins.scala file:

lazy val cobertura4sbt = "de.johoop" % "cobertura4sbt" % "1.0.0"

If you don't have this file yet, just create it like so:

class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
  lazy val cobertura4sbt = "de.johoop" % "cobertura4sbt" % "1.0.0"
}

Using cobertura4sbt in your project

Just mix in the trait Cobertura with your project definition and override any of the configuration properties (look them up in CoberturaProperties), for example like this (in your project/build/Project.scala file):

import de.johoop.cobertura4sbt._

class Project(info: ProjectInfo) extends DefaultProject(info) with Cobertura {
  // ...
}

That's it! You now have the additional actions cobertura-instrument, cobertura-report-html and cobertura-report-xml at your disposal.

One warning though: cobertura4sbt currently instruments your classes in-place. If you need instrumentation-free classes for packaging, you have to clean and recompile before packaging.

Some pitfalls when using Cobertura with SBT

SBT interactive mode problems

When covering code in SBT's interactive mode, Cobertura won't ever save its coverage information until you exit interactive mode. This means that you have to exit SBT interactive mode at least once just before generating any Cobertura reports.

Source path

The source path is currently set to mainJavaSourcePath by default. Please override the source path property for your purposes, for example like this:

override lazy val coberturaSourcePaths = List(mainScalaSourcePath)

The Cobertura report will also only be able to show you the sources to your classes if you follow the Java package directory tree conventions (which aren't mandatory in Scala).

License

This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html

Updated