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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:07:50  来源:igfitidea点击:

Run ScalaTest tests in parallel

scalasbtscalatest

提问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 a Distributorto the Suites you specify with -s. Runnerwill set up a thread pool to execute any Suites passed to the Distributor's putmethod in parallel.

如果您-P在命令行中包含,Runner则将 a 传递 DistributorSuite您用 指定的s -sRunner将设置一个线程池以并行执行Suite传递给Distributorput方法的任何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.

它还将运行在隔离的测试,所以beforeafter将在每个线程中运行。在ParallelTestExecutionRunner的文档中查看更多信息。

In SBT, to use the flag, add this to build.sbt:

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

testOptions in Test += Tests.Argument("-P")