Last Updated: February 25, 2016
·
4.109K
· sksamuel

Scala Code Coverage

In traditional code coverage tools, line coverage has been the main metric. This is fine for languages such as Java which are very verbose and very rarely have more than one statement per line, and more usually have one statement spread across multiple lines.

In powerful, expressive languages like Scala, quite often multiple statements, or even branches are included on a single line, eg a very simple example:

if (age < 18) "No beer" else "Beer for you"

If you had a unit test that ran through the value 18 you would get 100% line coverage yet you only have 50% statement coverage.

Let's expand this example out to be multi-facted, albeit somewhat contrived:

if (religion == "Pentecostalist") "No beer" else if (age < 18) "Underage" else "Beer for you"

Now we would get 100% code coverage for passing in the values ("Buddist", 34) because some code on that line would have been executed.

Other scala code coverage tools use bytecode instrumentation which gets around this problem. Tools such as https://github.com/sbt/jacoco4sbt integrate very well with Scala and give you accurate coverage, but suffer from two problems.

  1. Scala generates lots of bytecode that you, as an application developer, don't care about. This can be mitigated by the tool being programmed with cases to ignore.
  2. There's no way to go back from the bytecode into the source as Scala is a much higher level of abstraction than what bytecode can represent. In Java this is less of an issue as its closer to a 1-1 mapping of bytecode and source level constructs.

Scoverage is a code coverage tool that I have written and is available on github and is Apache licensed.
https://github.com/scoverage/scalac-scoverage-plugin

The focus is on statement coverage, and doesn't even include line coverage as a metric. This is a paradigm shift that we hope will become popular. The project has been tested for a few months at a major bank where I used to work so hopefully most of the bugs have been ironed out. The output is compatible with cobertura so should slot into most other tools like sonar.

1 Response
Add your response

Excellent

over 1 year ago ·