specs2 scala 测试中 OneAppPerSuite 的替代方案

Alternative of OneAppPerSuite in specs2 scala testing

我正在使用 specs2 编写单元测试用例,我的应用程序针对每个测试实例启动和停止。

import org.specs2.mutable._

class HelloWorldSpec extends Specification {

  "The 'Hello world' string" should {
    "contain 11 characters" in new WithApplication {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in new WithApplication {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in new WithApplication {
      "Hello world" must endWith("world")
    }
  }
}

如文档中所述,每个测试用例应用程序都会启动和停止。

我从 link 中找到了解决方法。每个测试 Class.

应用程序只初始化一次(我还没有测试过)
import org.specs2.mutable._

class HelloWorldSpec extends Specification {sequential

  step(Play.start(App)) //supposedly App is iniatilized

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }
  }
  step(Play.stop())
}

但是如果我们有多个 类 并且我们想要应用程序的单个启动和停止怎么办。

import org.specs2.mutable._

class HelloWorldSpec extends Specification {sequential

  step(Play.start(App)) //supposedly App is iniatilized

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }
  }
  step(Play.stop())
}

import org.specs2.mutable._

class HitchHikerSpec extends Specification {sequential

  step(Play.start(App)) //supposedly App is iniatilized

  "The 'Hitch Hiker' string" should {
    "contain 11 characters" in {
      "Hitch Hiker" must have size(11)
    }
    "start with 'Hitch'" in {
      "Hitch Hiker" must startWith("Hitch")
    }
    "end with 'Hiker'" in {
      "Hitch Hiker" must endWith("Hiker")
    }
  }
  step(Play.stop())
}

我将如何启动和停止一次应用程序?

scalatest 中使用 OneAppPerSuite 实施了类似的解决方案。 这是 link 和示例。

import play.api.test._
import org.scalatest._
import org.scalatestplus.play._
import play.api.{Play, Application}
import play.api.inject.guice._

// This is the "master" suite
class NestedExampleSpec extends Suites(
  new OneSpec,
  new TwoSpec,
  new RedSpec,
  new BlueSpec
) with OneAppPerSuite {
  // Override app if you need an Application with other than non-default parameters.
  implicit override lazy val app: Application =
    new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build()
}

// These are the nested suites
@DoNotDiscover class OneSpec extends PlaySpec with ConfiguredApp
@DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredApp
@DoNotDiscover class RedSpec extends PlaySpec with ConfiguredApp

@DoNotDiscover
class BlueSpec extends PlaySpec with ConfiguredApp {

  "The OneAppPerSuite trait" must {
    "provide an Application" in {
      app.configuration.getString("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in {
      def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key)
      getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    "start the Application" in {
      Play.maybeApplication mustBe Some(app)
    }
  }
}

能否在specs2中实现类似的东西?

使用 specs2,您可以使用 specification references 做类似的事情:

class SuiteSpec extends Specification { def is = s2"""
  ${link(StartSpec).hide}
  ${ "first spec"  ~ new Spec1Spec }
  ${ "second spec" ~ new Spec2Spec }
  ${link(StopSpec).hide}
  """
}

object StartSpec extends Specification { def is = s2"""
  ${step(println("start"))}
  """
}

class Spec1Spec extends Specification { def is = s2"""
  example1 $e1
  """

  def e1 = { println("example1"); ok }
}

class Spec2Spec extends Specification { def is = s2"""
  example2 $e2
  """

  def e2 = { println("example2"); ok }
}

object StopSpec extends Specification { def is = s2"""
  ${step(println("stop"))}
  """
}

那么如果你 运行:

testOnly *Suite* -- all

您应该会看到打印出以下几行:

start
example1
example2
stop