我正在使用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")
}
}
}
如每个测试用例的文档中所述,应用程序会启动和停止。
我从链接中找到了一个解决方法。每个测试类只会初始化一次应用程序(我还没有测试过)。
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
。这里是链接和示例。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中是否可以实现类似的功能?