scala 并行运行 ScalaTest 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15752681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run ScalaTest tests in parallel
提问by earldouglas
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:
如何并行运行测试(通过 sbt)?理想情况下,我希望执行顺序在标准输出上产生以下内容:
before
1a
2a
2b
1b
after
回答by Alex Yarmula
Use ParallelTestExecutionand a -Pcommand-line argument to the Runnerto make them run in parallel:
使用ParallelTestExecution和 的-P命令行参数Runner使它们并行运行:
import org.scalatest.{ParallelTestExecution, BeforeAndAfterAll, FunSuite}
class ParallelizableSpec extends FunSuite with BeforeAndAfterAll with ParallelTestExecution {
...
}
Note that -P is required. From the source:
请注意 -P 是必需的。从来源:
If you include
-Pon the command line,Runnerwill pass aDistributorto theSuites you specify with-s.Runnerwill set up a thread pool to execute anySuites passed to theDistributor'sputmethod in parallel.
如果您
-P在命令行中包含,Runner则将 a 传递Distributor给Suite您用 指定的s-s。Runner将设置一个线程池以并行执行Suite传递给Distributor的put方法的任何s 。
It will also run the tests in isolation, so beforeand afterwill be run in each thread. See more in the docs for ParallelTestExecutionand Runner.
它还将运行在隔离的测试,所以before和after将在每个线程中运行。在ParallelTestExecution和Runner的文档中查看更多信息。
In SBT, to use the flag, add this to build.sbt:
在 SBT 中,要使用该标志,请将其添加到build.sbt:
testOptions in Test += Tests.Argument("-P")

