Dariusz Mydlarz

Dariusz Mydlarz

Java, Distributed Systems & Software Engineering

QR code

How To Exclude Scala Tests With Tagging?

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 taggedAs method in the test declaration along with certain Tag object. For instance SlowTest like below.

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 you can exclude the specific tagged tests with a parameter passed to testOnly task in sbt.

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

Disable test suite

If you want to disable all tests in a 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 {
    // ...
}

Now just pass the annotation full name to testOnly task as exclude parameter.

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.