2016-06-17 60 views
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中可以實現類似的東西嗎?

回答

1

隨着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 
+0

謝謝對於答案,我試圖實現類似的解決方案,但使用BeforeAfterA將開始和停止應用程序。 但是,在應用程序啓動之前首先執行鏈接類會出現問題 – perfectus

相關問題