Given the following test suite:
class ParallelizeMe extends FunSuite with BeforeAndAfterAll {
override def beforeAll() = println("before")
override def afterAll() = println("after")
test("test 1") {
println("1a")
Thread.sleep(3000)
println("1b")
}
test("test 2") {
println("2a")
Thread.sleep(1000)
println("2b")
}
}
How can I run the tests (via sbt) in parallel? Ideally, I want the order of execution to produce the following on stdout:
before
1a
2a
2b
1b
after
sbt maps each test class to a task. sbt runs tasks in parallel and within the same JVM by default.
ScalaTest is one of the most popular, complete and easy-to-use testing frameworks in the Scala ecosystem. Where ScalaTest differs from other testing tools is its ability to support a number of different testing styles such as XUnit and BDD out of the box.
Use ParallelTestExecution
and a -P
command-line argument to the Runner
to make them run in parallel:
import org.scalatest.{ParallelTestExecution, BeforeAndAfterAll, FunSuite}
class ParallelizableSpec extends FunSuite with BeforeAndAfterAll with ParallelTestExecution {
...
}
Note that -P is required. From the source:
If you include
-P
on the command line,Runner
will pass aDistributor
to theSuite
s you specify with-s
.Runner
will set up a thread pool to execute anySuite
s passed to theDistributor
'sput
method in parallel.
It will also run the tests in isolation, so before
and after
will be run in each thread. See more in the docs for ParallelTestExecution and Runner.
In SBT, to use the flag, add this to build.sbt
:
testOptions in Test += Tests.Argument("-P")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With