我想对我的测试套件进行分区,并按照一个分区的测试不与其他分区的测试交错的方式运行它们。这是在SBT,ScalaTest中执行此操作的方法吗?
答案 0 :(得分:8)
有两个级别的并行可用于测试 - SBT找到的所有测试套件可以并行或顺序运行。您可以使用此SBT设置控制它:
parallelExecution in Test := false
默认值为true。
此外,每个测试服可以并行或顺序执行其测试用例。您可以在测试框架中控制它。例如,在Specs2中,您可以在测试类中添加sequential
:
class MyTest extends Specification {
sequential
// these test cases will be run sequentially based on the setting in the class
"test 1" should {
"do something" in {
...
}
"do something else" in {
...
}
}
}
我不知道ScalaTest是否有类似的设置,似乎之前并不存在。
有时,过滤哪些测试应根据其名称顺序运行(例如在SBT中)也很有用。你可以这样做:
object EventHubBuild extends Build {
lazy val root = Project(id = "root",
base = file("."))
.aggregate(common, rest, backup)
/**
* All unit tests should run in parallel by default. This filter selects such tests
* and afterwards parallel execution settings are applied.
* Thus don't include word 'Integration' in unit test suite name.
*
* SBT command: test
*/
def parFilter(name: String): Boolean = !(name contains "Integration")
/**
* Integration tests should run sequentially because they take lots of resources,
* or shared use of resources can cause conflicts.
*
* SBT command: serial:test
**/
def serialFilter(name: String): Boolean = (name contains "Integration")
// config for serial execution of integration tests
lazy val Serial = config("serial") extend(Test)
lazy val rest = Project(id = "rest",
base = file("rest"))
.configs(Serial)
.settings(inConfig(Serial)(Defaults.testTasks) : _*)
.settings(
testOptions in Test := Seq(Tests.Filter(parFilter)),
testOptions in Serial := Seq(Tests.Filter(serialFilter))
)
.settings(parallelExecution in Serial := false : _*)
.settings(restSettings : _*) dependsOn(common % "test->test;compile->compile", backup)
//// the rest of the build file ...
}
从https://github.com/pgxcentre/eventhub/blob/master/project/Build.scala复制。这在SBT中引入了一个任务test:serial
。
官方文档的更多详情:http://www.scala-sbt.org/0.13/docs/Parallel-Execution.html
答案 1 :(得分:2)
这是我发现的最简单的方法:
创建一个新的Scala类:
class TestController extends Suites (
new TestSuiteToRunFirst,
new TestSuiteToRunSecond,
new AnotherTestSuite
)
测试套件(类)将按照您在上面列表中添加它们的顺序运行。从现在开始,当您添加新的测试类时,将其添加到上面的TestController类声明中。
您需要做的另一件事: 在您自己的测试类定义中,用@DoNotDiscover注释每个类。这样可以防止它们由测试运行程序自动发现,然后由TestController再次运行。
@DoNotDiscover
class TestSuiteToRunFirst extends FlatSpec {
...
}