Its always good to have a fine test coverage in any project. I am working on Play framework & this time i’ve tried to integrate the JaCoCo with my play scala project & it’s cool to have it in my project.
I am going to elaborate a few easy steps to integrate the JaCoCo in Play Scala project.
1. Add the following lines of code in your plugin.sbt file. You’ve to add the plugin dependency to your project’s plugins.sbt.
libraryDependencies ++= Seq( "org.jacoco" % "org.jacoco.core" % "0.5.7.201204190339" artifacts(Artifact("org.jacoco.core", "jar", "jar")), "org.jacoco" % "org.jacoco.report" % "0.5.7.201204190339" artifacts(Artifact("org.jacoco.report", "jar", "jar"))) addSbtPlugin("de.johoop" % "jacoco4sbt" % "1.2.3")
2. Have this import in your Build.scala file.
3. Add the following line in your Build.scala file.
lazy val jacoco_settings = Defaults.defaultSettings ++ Seq(jacoco.settings: _*)
This line adds the jacoco settings to the variable jacoco_settings.
4. Add these settings in to your Play project in Build.scala file as
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA, settings = jacoco_settings).settings( parallelExecution in jacoco.Config := false)
parallelExecution in jacoco.Config := false makes sure that the ParallelExexution is false during testing.(Look at Point 7)
Note that we’ve a model class with name Test.scala & a test class DemoTest.scala for now.
5. That’s it. Lets run the JaCoCo with command jacoco:cover
6. You’ll find the html file with name index.html which would give you the full test report. You’ll find this file in this path /target/scala-2.9.1/jacoco/html
This is how your coverage report would look like :
7. You can check whether the parallel execution if false or not as
Get the CodeCoverage here