14

sbt's test-only command can be used to run the tests found in a specific test class. With JUnit tests you can use test-only to run specific methods on a test class e.g. test-only mypackage.MyTestClass.test1Equals1 to run just that method.

Is such a thing possible with scalatest's more free-form test syntax, presumably by working out the name it uses internally to reference a specific test? If it isn't possible in FreeSpec (which is easy to imagine given its nature) is there a way to do it with a simpler testing approach like FunSuite?

adamnfish
  • 9,719
  • 4
  • 28
  • 40
  • 4
    There's a feature open for this on sbt. Part of this involves how we fingerprint tests to run and the framework integration. It was scheduled for 0.13.2, but was just too hairy to finish. I'm hoping we'll have time for it in 0.13.3. – jsuereth Mar 05 '14 at 21:40
  • 1
    Thanks @jsuereth. When it makes it into a release put an answer down here if you remember :-) – adamnfish Mar 06 '14 at 12:17
  • @jsuereth, is [this](https://github.com/sbt/sbt/issues/911) the enhancement that you are referring to? – Erik J. Sturcke Mar 12 '14 at 14:43

2 Answers2

11

For a general solution we need to wait for sbt#911, but apparently ScalaTest 2.1.3 has a support for running a specific test. Seth says:

This is now supported in ScalaTest 2.1.3 with:

test-only *MySuite -- -z foo

to run only the tests whose name includes the substring "foo". For exact match rather than substring, use -t instead of -z.

Community
  • 1
  • 1
Eugene Yokota
  • 90,473
  • 43
  • 204
  • 301
  • Worth noting that generally, `sbt`'s `test-only` passes arguments to the underlying test runner and `scalatest` now supports `-z ""`. Other test frameworks will take different arguments. – adamnfish Oct 31 '16 at 16:00
  • 2
    Using ScalaTest 3.0.5 this seems to have changed to `testOnly` instead of `test-only`. – Florian Baierl Jan 29 '19 at 07:46
1

To run a test:

sbt test-only com.path.to.UnitTestSpec

To run a particular integration test:

sbt it:test-only com.path.to.IntegrationTestSpec

Priyank Desai
  • 3,257
  • 1
  • 23
  • 17
  • Although this answer does not answer OPs question, it's relevant and shows in the search results. To run from terminal, it's `sbt "testOnly com.path.to.UnitTestSpec"`. Quotes make sure that the argument is passed on. – Niks Jan 03 '20 at 09:27