14

I want to disable certain automated tests tagged as "Slow" by default but allow the user to enable their execution with a simple command line. I imagine this is a very common use case.

Given this test suite:

import org.scalatest.FunSuite
import org.scalatest.tagobjects.Slow

class DemoTestSuite extends FunSuite {
  test("demo test tagged as slow", Slow) {
    assert(1 + 1 === 2)
  }

  test("demo untagged test") {
    assert(1 + 1 === 2)
  }
}

By default, sbt test will run both tagged and untagged tests.

If I add the following to my build.sbt:

testOptions in Test += Tests.Argument("-l", "org.scalatest.tags.Slow")

Then I get my desired default behavior where untagged tests run and the Slow tagged test will not run.

However, I can't figure out a command line option that will run the slow tests when I want to run them. I've done several searches and tried several examples. I'm somewhat surprised as this seems like a very common scenario.

clay
  • 18,138
  • 28
  • 107
  • 192
  • Have you checked [this article](http://alvinalexander.com/scala/scalatest-mark-tests-tags-to-include-exclude-sbt) out? – evan.oman Dec 19 '16 at 19:25
  • 1
    @evan058, yes, and that works, but it leaves the default behavior to run everything. I'd like the default to exclude slow tests. – clay Dec 19 '16 at 19:36
  • 1
    Have you tried `testOnly org.myorg.* -- -n org.scalatest.tags.Slow`? From [this page](http://www.scalatest.org/user_guide/using_scalatest_with_sbt), section "Include and Exclude Tests with Tags". – insan-e Dec 19 '16 at 20:33

1 Answers1

11

I had a similar issue: I wanted to have tests that are disabled by default, but run in the release process. I solved it by creating a custom test configuration and setting testOptions in different scopes. So adapting this solution to your case, it should be something along these lines (in your build.sbt):

lazy val Slow = config("slow").extend(Test)
configs(Slow)
inConfig(Slow)(Defaults.testTasks)

Now by default exclude slow tests:

testOptions in Test += Tests.Argument("-l", "org.scalatest.tags.Slow")

But in the Slow scope don't exclude them and run only them:

testOptions in Slow -= Tests.Argument("-l", "org.scalatest.tags.Slow")
testOptions in Slow += Tests.Argument("-n", "org.scalatest.tags.Slow")

Now when you run test in sbt, it will run everything except slow test and when you run slow:test it will run only slow tests.

laughedelic
  • 6,230
  • 1
  • 32
  • 41
  • 1
    Note: `lazy val Slow` goes into the top level, for multi-project builds, while `configs` and `inConfig` are per-project. See also https://www.scala-sbt.org/1.x/docs/Testing.html#Additional+test+configurations+with+shared+sources – Suma Jan 17 '22 at 15:47