We often want to exclude some test cases. ScalaTest has @Ignore annotation to exclude that test case to be run as well as JUnit. But how can we include or exclude test cases in more fine-grained way?

ScalaTest provides the feature called Tagging. By using tagging, you can specify which test cases to be run and which are not. You can use this feature in both FlatSpec and WordSpec.

For example, assuming you want to attach a tag to some test cases to specify them as slow test, you can create your own tag as follows.

import org.scalatest.Tag
object SlowTest extends Tag("com.lewuathe.SlowTest")

You can use the tag in each test cases.

class SomeTest extends WordSpec {
  "A Class" should {
    "do heavy task" taggedAs(SlowTest) in {
      // Doing some heavy test
    }
  }
}

There might be some cases when you don’t want to run these heavy test because they take a lot of time. Of course it’s the best we can improve test case performance to be done in reasonable time but just excluding can be a workaround. Once you tagged them as SlowTest both including and excluding are easy.

Excluding

$ sbt "test-only -- -l com.lewuathe.SlowTest"

Including (just running only test cases tagged as SlowTest)

$ sbt "test-only -- -n com.lewuathe.SlowTest"

You can which test case to be run arbitrary by using tagging of ScalaTest.

Thanks.

Reference