TIL: run specific tests with sbt testOnly

Running a specific test suite or a single test is no doubt a useful and frequent requirement. Here’s how to do that with sbt and ScalaTest.

Run specific test suites by passing them as arguments to testOnly:

> sbt testOnly com.blah.StackSpec com.blah.ATSomeSpec

The testOnly task also accepts wildcards, which is how I usually specify the suites:

> sbt testOnly **.StackSpec **.AT*Spec

Sometimes a test suite is big and/or slow, and you really want to run a single test or a subset of related tests. The following will run all tests which contain “for cars” in their names (-- is required to mark the start of parameters passed verbatim to the test runner):

> sbt testOnly **.StackSpec -- -z "for cars"

Executing the full sbt command over and over again on every test run is rather slow. Instead, launch the sbt command prompt (also know as interactive mode) and run the tasks from there. It will incur the startup overhead only once.

> sbt
[info] Loading project definition from /my/blah/project
[info] Set current project to blah (in build file:/my/blah/)
[blah] $ testOnly **.StackSpec -- -z "for cars"
# [... runs tests ...]
[blah] $ testOnly **.StackSpec -- -z "for cars"
# [... runs tests again, without startup overhead ...]

The commands above work for ScalaTest. Similar filtering parameters are available for specs2 and ScalaCheck.


← back to main page