2013-06-05 8 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

@ ओम-नाम-नाम यह केवल बताता है कि प्रत्येक मामले के आसपास कोड कैसे निष्पादित किया जाए। – lambdas

+0

ओह, मेरा मतलब था 'टेम्पलेट' या 'ग्लोबल सेटअप/टियरडाउन' अनुच्छेद (डेटाबेस सेटअप/क्लीनअप वाला एक) :-) –

उत्तर

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 मिश्रण और beforeAll और afterAll तरीकों को लागू:

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

ठीक है, जैसा कि मेरी टिप्पणी में बताया गया है, मुझे वास्तव में यह वही समस्या थी। मुझे Unfiltered endpoints का परीक्षण करने की आवश्यकता थी, और प्रत्येक spec के लिए सबसे अच्छा तरीका एक अंतराल के साथ एक अनफिल्टर सर्वर को स्टार्टअप करना था, spec चलाएं और फिर सर्वर को बंद करें।

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

ठीक है यह एक पुराने सवाल यह है कि, लेकिन यह किसी की मदद कर सकता है।

मैं प्ले फ्रेमवर्क का उपयोग कर रहा हूं। मेरी परीक्षणों में मैं इस्तेमाल किया 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") 
    } 
} 
संबंधित मुद्दे