2013-06-05 97 views
12

後,我在這幾個案件簡單的規範:執行代碼之前和規範

class MySpec extends Specification { 

    "Something" should { 

    "case 1" in { 
     ... 
    } 

    "case 2" in { 
     ... 
    } 
    } 
} 

現在我需要啓動應用程序,運行所有的情況下,並關閉應用程序。開始/停止應用程序非常耗時,我不希望它發生在每個案例中。

如何在病例開始之前以及所有病例完成之後運行代碼?

+0

[向下滾動,直到在此之前,文檔頁/後段(http://etorreborre.github.io/specs2/guide/org.specs2 .guide.Structure.html) –

+0

@ om-nom-nom它只解釋瞭如何執行每個案例的代碼。 – lambdas

+0

哎呀,我的意思是'Template'或'Global setup/teardown' paragraph(帶有數據庫設置/清理的那個):-) –

回答

29

我想出了基於cmbaxter答案的以下解決方案。

import org.specs2.specification.Step 

trait BeforeAllAfterAll extends Specification { 
    // see http://bit.ly/11I9kFM (specs2 User Guide) 
    override def map(fragments: =>Fragments) = 
    Step(beforeAll)^fragments^Step(afterAll) 

    protected def beforeAll() 
    protected def afterAll() 
} 

然後在Specification混合BeforeAllAfterAll和實施beforeAllafterAll方法:

class MySpec extends Specification with BeforeAllAfterAll { 

    def beforeAll() { 
    println("Doing setup work...") 
    } 

    def afterAll() { 
    println("Doing shutdown work...") 
    } 

    "Something" should { 

    "case 1" in { 
     ... 
    } 

    "case 2" in { 
     ... 
    } 
    } 
} 

最後,提取初始化規範之間分享:

trait InApplication extends BeforeAllAfterAll { 
    def beforeAll() { 
    println("Doing setup work...") 
    } 

    def afterAll() { 
    println("Doing shutdown work...") 
    } 
} 

class MySpec extends Specification with InApplication { 

    "Something" should { 

    "case 1" in { 
     ... 
    } 

    "case 2" in { 
     ... 
    } 
    } 
} 
+1

這真的很有幫助謝謝。現在如果我想爲所有規格創建一個全局設置? –

7

好的,正如我在評論中提到的那樣,我實際上也有同樣的問題。我需要測試未經過濾的端點,每種規範的最佳方法是使用單個端點啓動未經過濾的服務器,運行規範然後關閉服務器。爲了實現這一點,我首先定義一個類似的基本規格:

import org.specs2.mutable.Specification 

abstract class SpecificationBase extends Specification{ 
    //Do setup work here 
    step{ 
    println("Doing setup work...") 
    success 
    } 

    //Include the real spec from the derived class 
    include(spec) 

    //Do shutdown work here 
    step{ 
    println("Doing shutdown work...") 
    success 
    } 

    /** 
    * To be implemented in the derived class. Returns the real specification 
    * @return Specification 
    */ 
    def spec:Specification 
} 

基本上,這個基類組裝完整的規範作爲設置步驟和拆卸步驟與真實規範(在具體的規範類中定義)夾在中間。因此,使用這個基類的測試是這樣的:

class MySpec extends SpecificationBase{ def spec = 
    new Specification{ 
    "A request to do something" should{ 
     "be successful in case 1" in { 
     println("Testing case 1") 
     success 
     } 
     "be successful in case 2" in { 
     println("Testing case 2") 
     success 
     }  
    } 
    } 
} 

當你運行它,你會看到:

Doing setup work... 
Testing case 1 
Testing case 2 
Doing shutdown work... 

它並不完美,但它的作品。是否有另一種(和可能更清潔/更好)的方式來做到這一點?可能,但這是您可以考慮使用的一種解決方案。

+0

謝謝!我已經開發出了基於你的更清潔的解決方案,並將其發佈爲答案。 – lambdas

1

確定這是一個老問題,但它可能有助於某人。

我正在使用Play框架。在我的測試中,我使用org.scalatest.BeforeAndAfterAll

例子:

import org.scalatest.BeforeAndAfterAll 

class MySpec extends PlaySpec with BeforeAndAfterAll { 

    "Some test" must { 
    "print a text" in { 

     println("Some test") 
     2 mustBe 2 
    } 
    } 

    override def beforeAll(): Unit = { 
    println("Before") 
    } 

    override def afterAll(): Unit = { 
    println("After") 
    } 
} 
相關問題