2
我正在使用specs2編寫單元測試用例,並且我的應用程序已針對每個測試實例啓動和停止。OneAppPerSuite在specs2 scala測試中的替代方案
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的解決方法。應用程序僅初始化一次(我還沒有爲每個測試類進行測試)。
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中可以實現類似的東西嗎?
謝謝對於答案,我試圖實現類似的解決方案,但使用BeforeAfterA將開始和停止應用程序。 但是,在應用程序啓動之前首先執行鏈接類會出現問題 – perfectus