gpt4 book ai didi

scala - 并行运行 ScalaTest 测试

转载 作者:行者123 更新时间:2023-12-03 20:18:29 24 4
gpt4 key购买 nike

鉴于以下测试套件:

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)?理想情况下,我希望执行顺序在标准输出上产生以下内容:
before
1a
2a
2b
1b
after

最佳答案

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

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.



它还将单独运行测试,因此 beforeafter将在每个线程中运行。在 ParallelTestExecution 的文档中查看更多信息和 Runner .

在 SBT 中,要使用该标志,请将其添加到 build.sbt :
testOptions in Test += Tests.Argument("-P")

关于scala - 并行运行 ScalaTest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15752681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com