
Scala Code Coverage: Maven + TravisCI + Coveralls
If you’ve been programming in Java for a while and haven’t tried Scala, you’re really missing out! Scala is a concise, no nonsense functional/OO language on the JVM. Scala’s libraries are built on top of Java’s expansive JDK, so many awesome libraries are inter-operable with Scala and Java.
With that said, I found myself at a loss. I needed a way to ensure adequate code coverage for my Twitter library, TW4S, but there weren’t any viable Scala-native options to be found. I thought, “OK, how would I do this in Java?”
TravisCI + Coveralls
TravisCI is the premier testing platform for projects hosted on GitHub. It’s free, super easy to setup, and tests your project for every commit pushed. What I like most is the setup. TravisCI knows the testing sematics of a bevy of languages. For Java/Scala, it speaks maven/junit like a pro.
Through a bit of searching, I discovered Coveralls, a free code coverage platform for open source projects. It is heavily coupled with TravisCI and can be launched after every successful Travis build. Perfect! All I needed to do was to setup Coveralls to run after my Travis builds pass.
Cobertura + Maven
Coveralls needs a code coverage tool to receive reports from. I chose Cobertura. It has a built-in maven directive to send reports right to coveralls. Cobertura works at the byte-code level. Since Scala compiles to byte-code, there’s no special setup for Scala. It works just as effectively for Scala or Java.
I added the following to my maven pom.xml, under the build > plugins section.

The first point to notice is that I had to define both a sourceDirectory and a sourceTestDirectory. I follow maven convention, but since Cobertura assumes a Java maven project, I had to let it know where the source files were.
Next, I had to define the Cobertura maven plugin. The configuration is standard except for the exclusion statements. By default, Cobertura includes .class files in its coverage assessment. This is made messier by Scala’s .class file naming conventions. The exclude statements will ignore those files.
Finally, I added the Coveralls maven plugin. This allows me to send Cobertura’s output to Coveralls.
TravisCI Build Hook
We’re almost done! All we need to do is to make TravisCI kickoff Cobertura and send the results to Coveralls.

That’s it. Commit all your changes, and if your project is enabled on TravisCI and Coveralls, you will get results!

Looks like I have some work to do. If you want to look around at my setup, feel free to go to TW4S’s github page.
Email me when Cam Cook publishes or recommends stories