【发布时间】:2020-12-20 07:13:47
【问题描述】:
我正在使用一个套件(Scalatest 版本:3.2.2),其中包含多个测试套件:
class SuiteMixedSequentialParallel
extends Stepwise(
new TestInParallel,
new TestSequentially
)
现在,我希望TestInParallel 中的所有测试都应该并行执行,TestSequentially 中的所有测试都应该顺序执行。
因此我从ParallelTestExecution扩展TestInParallel
class TestInParallel extends AnyFunSuite with ParallelTestExecution {
(0 to 10).foreach(i =>
test(s"$i") {
Thread.sleep(500)
println(s"TestInParallel $i")
}
)
}
class TestSequentially extends AnyFunSuite {
(0 to 10).foreach(i =>
test(s"$i") {
Thread.sleep(200)
println(s"TestSequentially $i")
}
)
}
当我运行sbt testOnly TestInParallel 时,所有测试都是并行执行的。
但:
当我运行sbt testOnly SuiteMixedSequentialParallel 时,所有测试都按顺序执行。
当我运行SuiteMixedSequentialParallel 时,是否有人提示我在TestInParallel 中的测试将并行运行?
套件TestInParallel 和TestSequentially 仍应按顺序运行。只是 TestInParallel 中的测试应该并行运行。
【问题讨论】:
-
旁注:如果您想要
Ints 的范围,您可以使用List.range(0, 10)(第一个数字包含在内,最后一个数字不包含在内)。 -
谢谢!很好的提示。嗯,现在我渴望相应地更改示例中的代码。但是,您的评论将不再有意义。所以,我让
List.tabulate(10)(identity)照原样。 -
顺便说一句,更简单的可能是
for (i <- 0 to 10) { test(...) }。无需编辑您的代码。或者,评论你编辑它并编辑它,任何阅读 cmets 的人都会理解。 :) -
在此 cmets 的帮助下编辑了代码示例,使示例更简洁。
标签: scala parallel-processing scalatest