Specs2步骤不按顺序执行

时间:2014-08-29 13:27:57

标签: scala specs2

我正在尝试使用多个测试来执行规范,这些测试都在同一个Play应用程序中运行,而不是每个测试的单独应用程序。

因此我有以下代码可以打印:

Play app started
[info] PlayRunningImmutableSpec
[info]     
[info]     + 200 status expected
[info]     
[info]     + 404 status expected
Play app stopped

但是打印:

Play app started
Play app stopped
[info] PlayRunningImmutableSpec
[info]     
[info] 
[info]     ! 200 status expected
[error]  ConnectException: : Connection refused: /127.0.0.1:19001 to http://127.0.0.1:19001/

我正在使用Typesafe Activator 1.2.10,其中包括Play 2.3.3和Specs2 2.3.12

以下代码出了什么问题,取而代之的是什么呢?

import org.specs2.Specification
import org.specs2.execute.Result
import org.specs2.specification.Step
import org.specs2.time.NoTimeConversions
import play.api.Play
import play.api.Play.current
import play.api.http.{HeaderNames, HttpProtocol, Status}
import play.api.libs.ws.WS
import play.api.test._

class PlayRunningImmutableSpec extends Specification with NoTimeConversions with PlayRunners with HeaderNames with Status with HttpProtocol with DefaultAwaitTimeout with ResultExtractors with Writeables with RouteInvokers with FutureAwaits {

  override def is = s2"""
    ${Step(beforeAll)}

    200 status expected      $e1

    404 status expected      $e2

    ${Step(afterAll)}
  """

  def e1: Result = {
    await(WS.url(s"http://127.0.0.1:${Helpers.testServerPort}").get()).status === 200
  }

  def e2: Result = {
    await(WS.url(s"http://127.0.0.1:${Helpers.testServerPort}/missing").get()).status === 404
  }

  lazy val app = FakeApplication()

  private def beforeAll = {
    Play.start(app)
    println("Play app started")
  }

  private def afterAll = {
    Play.stop()
    println("Play app stopped")
  }
}

编辑:

我意识到我的错误在于使用play.api.Play.start方法,现在有一个简单的特性来处理一次启动和关闭:

trait PlayServerRunning extends SpecificationLike {

  override def map(fs: => Fragments): Fragments = Step(beforeAll) ^ fs ^ Step(afterAll)

  private lazy val server = TestServer(Helpers.testServerPort)

  private def beforeAll = {
    server.start()
  }

  private def afterAll = {
    server.stop()
  }

}

1 个答案:

答案 0 :(得分:2)

提出建议。测试是并行执行的(根据执行上下文的实现细节)。

如果您的测试需要按顺序进行,则必须以这种方式进行注释。 e.g:

"X" should {
  sequential

  "exp1" in { ... }
  "exp2" in { ... }
}