加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

并行运行ScalaTest测试

发布时间:2020-12-16 09:16:00 所属栏目:安全 来源:网络整理
导读:给出以下测试套件: 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
给出以下测试套件:

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

}

如何并行运行测试(通过sbt)?理想情况下,我希望在stdout上执行下列命令:

before
1a
2a
2b
1b
after

解决方法

对Runner使用ParallelTestExecution和-P命令行参数使其并行运行:

import org.scalatest.{ParallelTestExecution,BeforeAndAfterAll,FunSuite}
class ParallelizableSpec extends FunSuite with BeforeAndAfterAll with ParallelTestExecution {
   ...
}

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

If you include -P on the command line,Runner will pass a
Distributor to the Suites you specify with -s. Runner will set
up a thread pool to execute any Suites passed to the Distributor‘s
put method in parallel.

它也将独立运行测试,所以在每个线程之前和之后将运行测试.请参阅ParallelTestExecution和Runner的文档.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读