Excluding tests from coverage report with sbt coverage

463 views Asked by At

I have a test that is non-functional, so I do not want it to affect the coverage report. Other than putting it in a separate project, is there a way to exclude it from the report when running

sbt clean coverage test coverageReport

As it stands, it is obscuring missing coverage by other tests.

To explain better I've made a sample project in GitHub. There are two test classes - MainSpec and DoubleSpec. I've commented out the test in DoubleSpec to show that the test in MainSpec gives 100% coverage. However, it's obscuring that DoubleSpec is "broken". MainSpec is not testing the functionality, only checking the return type.

The example is contrived. I have a much more complex example which I cannot share.

I've tried using coverageExcludedFiles but this only excludes source from coverage, not tests.

1

There are 1 answers

3
Mario Galic On BEST ANSWER

Consider creating a ScalaTest tag

import org.scalatest.Tag
object ExcludeFromCoverage extends Tag("ExcludeFromCoverage")

based on which to exclude particular tests only when running coverage

  "double" should "return an Int" taggedAs ExcludeFromCoverage in {
    ...
  }

so now executing using testOnly like so

sbt clean coverage  "testOnly * -- -l ExcludeFromCoverage" coverageReport

will exclude "double" should "return an Int" from the coverage report. Note you can still execute all tests with sbt test.


Given above, then to still run tests tagged with ExcludeFromCoverage but not include them in coverage consider defining a custom command like so

commands += Command.command("testWithSmartCoverage") { state =>
  "clean" :: 
  "set coverageEnabled := true" :: 
  "testOnly * -- -l ExcludeFromCoverage" :: 
  "set coverageEnabled := false" ::
  "testOnly * -- -n ExcludeFromCoverage" :: 
  "coverageReport" :: 
  state
}