How To Exclude Scala Tests With Tagging?

Dariusz Mydlarz
1 min readNov 20, 2018

--

If you want to disable certain types of tests with Scala you can use tags.

Disable single test

To disable specific test cases you can use this code:

package com.dmydlarz.playground

object SlowTest extends Tag("com.dmydlarz.playground.SlowTest")

class PlaygroundSpec extends FlatSpecLike {
it should "run some slow tests" taggedAs SlowTest in {
// ...
}
}

Then just run below command to exclude the specific slow tests:

sbt "testOnly * -- -l com.dmydlarz.playground.LocalTest"

Disable test suite

If you want to disable whole class — including beforeAll and afterAll method you can use old good (or bad?) Java annotation.

package tags;

import org.scalatest.TagAnnotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@TagAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface LocalOnly {
}

And place it on the desired specs.

@tags.LocalOnly
class PlaygroundSpec extends FlatSpecLike with BeforeAndAfterAll {
// ...
}

Then just run:

sbt "testOnly * -- -l tags.LocalOnly"

Be aware!

Two things to be aware:

  1. Always use full package name when excluding the tests withing sbt command.
  2. Do not forget to double quote the phrase testOnly * -- -l <tag>. Otherwise it will not run as you expect it.

Originally published at dmydlarz.com on November 20, 2018.

--

--

Dariusz Mydlarz

Software Engineer keen on delivering best value for the business