Search code examples
playframeworkcobertura

How to enable code coverage in a Play-framework project using Cobertura?


I'm trying to get Cobertura to work for unit tests on a project that uses the Play framework. I'm following the instructions here:

https://www.playframework.com/modules/cobertura-2.4/home

I added the specified lines to the conf/application.conf file, but when I run my tests there is no code coverage report created in either of the places suggested. There's no code-coverage file created anywhere in my project.

(As an aside, I don't know what it's talking about when it says "start test in web interface", or mentions going to "localhost:9000/@tests" in a web browser -- when I go there after running activator run, the connection either times out or gives a 404.)

What else do I need to do to get Cobertura working? Some other pages mentioned the need to download the library and add it to the project, but I'm not sure where to put the library, or whether I can add it by adding something to the build.sbt file instead (my preferred method of adding libraries).


Solution

  • Those instructions no longer work because they're for an older version of Play and the cobertura module is no longer present in newer versions of Play.

    I was able to do it using Jacoco4SBT. Following the instructions found in jacoco4sbt's README, add the following lines to project/plugins.sbt:

    resolvers += Resolver.sonatypeRepo("public") // (not entirely sure this line is necessary)
    addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.3.0")
    

    Then add the following line to build.sbt:

    jacoco.settings
    

    You can then generate a code coverage report by running this command:

    activator jacoco:cover