ScalaTest组测试并按顺序运行

Jac*_* L. 7 testing integration-testing scala sbt scalatest

我想对我的测试套件进行分区,并按照一个分区的测试不与其他分区的测试交错的方式运行它们.这是在SBT,ScalaTest中执行此操作的方法吗?

yǝs*_*ǝla 8

有两个级别的并行可用于测试 - SBT找到的所有测试套件可以并行或顺序运行.您可以使用此SBT设置控制它:

parallelExecution in Test := false
Run Code Online (Sandbox Code Playgroud)

默认值为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 {
      ...
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

我不知道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 ...
}
Run Code Online (Sandbox Code Playgroud)

复制自https://github.com/pgxcentre/eventhub/blob/master/project/Build.scala.这test:serial在SBT中引入了一项任务.

来自官方文档的更多详细信息:http://www.scala-sbt.org/0.13/docs/Parallel-Execution.html


Joe*_*jr2 5

这是我发现的最简单的方法:

创建一个新的 Scala 类:

class TestController extends Suites (
  new TestSuiteToRunFirst,
  new TestSuiteToRunSecond,
  new AnotherTestSuite
)
Run Code Online (Sandbox Code Playgroud)

测试套件(类)将按照您在上面列表中添加它们的顺序运行。从现在开始,当您添加新的测试类时,您将把它添加到上面的 TestController 类声明中。

您需要做的另一件事是:在单独的测试类定义中,使用 @DoNotDiscover 注释每个类。这将防止测试运行器自动发现它们,然后由 TestController 再次运行。

@DoNotDiscover
class TestSuiteToRunFirst extends FlatSpec {
...
}
Run Code Online (Sandbox Code Playgroud)