执行测试的顺序错误

时间:2014-12-23 16:28:40

标签: scala scalatest specs2

我有以下测试代码:

import org.specs2.mutable.Specification
import org.specs2.specification.{BeforeExample, Before, Scope}

class TestSpec extends Specification with BeforeExample {

  def before = println("before!!!!")

  "Test" should {
    "run before 1" in {
      println("test 1")
      success
    }
    "run before 2" in {
      println("test 2")
      success
    }
    "run before 3" in {
      println("test 3")
      success
    }
  }

}

我期待的是:

before!!!!
test 1
before!!!!
test 2
before!!!!
test 3
...

但得到输出:

before!!!!
before!!!!
before!!!!
test 3
test 2
test 1Test should
run before 1
run before 2
run before 3

为什么这么奇怪的命令? 测试是以并行模式运行的吗?

在这个合成的例子中,没关系。但是如果before进行数据库清理或其他事情,则执行顺序会中断测试。

1 个答案:

答案 0 :(得分:2)

specs2默认执行并发测试(请参阅文档here)。添加sequential关键字以便按顺序执行测试:

class TestSpec extends Specification with BeforeExample {
 sequential
 // your tests
}