2
votes

I'm working on a legacy project that has been written in Scala using SBT. The unit tests have been written with ScalaTest. The problem is there is a lot of test classes that have compilation error.

I want to run only one of these test classes and I know for running only one test I should try something like this:

test:testOnly *myClass

But when I run this command, the SBT will try to compile the whole project and as I said, there's a lot of test classes that have compilation error. Is there any way to tell SBT exactly which classes need to be compiled and so the others will be ignored?

3
sbt testOnly name.DanielSmith.TestClassPedro Correia Luís

3 Answers

1
votes

You have as mentioned above the next option:

  • sbt testOnly *className : This will run all the tests defined in that class.
  • sbt testOnly *className-- -z "test-pattern" : It will run all the tests from that class which spec matchs the pattern defined.
1
votes

Consider defining a single-argument custom command in build.sbt like so

commands += Command.single("compileAndTestOnly") { (state, file) =>
  s"""set sources in Test := (sources in Test).value.filter(_.name.contains("$file"))""" ::
    "test" :: state
}

Now run it with compileAndTestOnly MySpec.scala. This command modifies Test \ sources setting to contain only a single file like so

sources in Test := (sources in Test).value.filter(_.name.contains(file))

Note sources in Compile is left untouched. Afterwards it executes test which in effect compiles and runs a single test.

This answer is inspired by 0__.

0
votes

It's impossible to instruct sbt to only compile the file that contains the test that matches your name, because it'll only know what name the test has after it's compiled -- in scala a class name and a file name don't have to have anything to do with another.

A source filter is not built-in in sbt. You will need a custom task along the lines of what @mario-galic has above.

Beware though, that will filter based on file name, not on class name.

I agree it would be a neat feature for sbt to run all tests it can even when the compilation of some files were to fail.