2

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 Answers1

1

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
}
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • That solves some of it but it doesn't run the MainSpec at all now. I would like the test in MainSpec to run but not be in the coverage report. – codevineyard Dec 30 '22 at 15:39
  • I've got it working by introducing a test script. I've updated the project so that it works. Thanks for the info about test tags, which were the important bit I didn't know. – codevineyard Dec 30 '22 at 16:24
  • Thanks - using custom commands is very neat! I've updated the repo and acknowledged your fantastic help. – codevineyard Dec 30 '22 at 18:55