scalaplayframeworksbtcode-coveragescoverage

How to exclude method in class from scoverage report?


I have few methods in my scala play framework application that I want to be excluded from scoverage report. Is there any way to achieve this? May be similar to using @Generated annotations for methods to be excluded as for Jacoco 0.8.2 release. Example:

class TestClass {
    @Generated
    def methodN = {} 
}

Or may be use something like excludeMethods += "TestClass.methodN, TestClass.methodX" in build.sbt file?


Solution

  • From the docs I'd say that without changing the source code you can exclude only class/package/file from your build tool

    // examples of scalac options from the docs
    // add as e.g. scalacOptions += "-P:scoverage:..." if you are using sbt
    
    -P:scoverage:excludedPackages:.*\.utils\..*;.*\.SomeClass;org\.apache\..*
    
    -P:scoverage:excludedFiles:.*\/two\/GoodCoverage;.*\/three\/.*
    

    but if you can modify the source code you can exclude whatever you want by putting the right comments around it

    // $COVERAGE-OFF$
    def methodIWantToIgnore = 2 + 2
    // $COVERAGE-ON$
    

    If it's the code you written yourself I would use comments, and if it was something spit by the codegen I would suggest the compiler options (scoverage works as a compiler plugin, slightly changing emitted bytecode).