并行运行ScalaTest测试

ear*_*las 15 scala sbt scalatest

鉴于以下测试套件:

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")
  }

} 
Run Code Online (Sandbox Code Playgroud)

如何并行运行测试(通过sbt)?理想情况下,我希望执行顺序在stdout上生成以下内容:

before
1a
2a
2b
1b
after
Run Code Online (Sandbox Code Playgroud)

Ale*_*ula 23

使用ParallelTestExecution-P命令行参数Runner使它们并行运行:

import org.scalatest.{ParallelTestExecution, BeforeAndAfterAll, FunSuite}
class ParallelizableSpec extends FunSuite with BeforeAndAfterAll with ParallelTestExecution {
   ...
}
Run Code Online (Sandbox Code Playgroud)

请注意,-P是必需的.从来源:

如果-P在命令行中包含,Runner则将a传递 DistributorSuite您指定的s -s.Runner将建立一个线程池来执行任何Suite在交给了S Distributorput并行方法.

它也将在隔离运行测试,所以beforeafter将在每个线程中运行.有关ParallelTestExecutionRunner的文档,请参阅更多内容.

在SBT中,要使用该标志,请将其添加到build.sbt:

testOptions in Test += Tests.Argument("-P")
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:使用sbt 0.13,您无法使用-P选项.sbt将自动处理并行执行. (13认同)
  • 谢谢,这就是我在寻找的东西.我希望我可以拥有之前和之后每次只运行一次,但这已足够接近了.作为参考,我将此添加到我的*build.sbt*:Test +中的test + = Tests.Argument(" - P")` (3认同)