2013-04-06 58 views
0

我目前正在嘗試運行2個不同類的相同測試用例,但遇到了setup()問題,我看到類似的問題,但還沒有看到解決方案用Spock進行常規測試,我一直無法弄清楚。爲groovy和spock中的不同類運行相同的測試

所以我基本上是用兩種不同的方法解決同樣的問題,所以相同的測試用例應該適用於這兩個類,我試圖保持不要重複自己(DRY)。

所以我成立了一個MainTest爲抽象類和MethodOneTest和MethodTwoTest作爲擴展抽象MainTest具體類:

import spock.lang.Specification 
abstract class MainTest extends Specification { 
    private def controller 

    def setup() { 
     // controller = i_dont_know.. 
    } 

    def "test canary"() { 
     expect: 
     true 
    } 

    // more tests 
} 

我的具體類是這樣的:

class MethodOneTest extends MainTest { 
    def setup() { 
     def controller = new MethodOneTest() 
    } 
} 

class MethodTwoTest extends MainTest { 
    def setup() { 
     def controller = new MethoTwoTest() 
    } 
} 

所以沒有人知道我可以從我的具體類MethodOneTest和MethodTwoTest中運行抽象MainTest中的所有測試嗎?如何正確實例化安裝程序?我希望我清楚。

回答

1

忘記控制器設置。當您爲具體類執行測試時,將自動執行超類的所有測試。例如。

import spock.lang.Specification 
abstract class MainTest extends Specification { 
    def "test canary"() { 
     expect: 
     true 
    } 

    // more tests 
} 

class MethodOneTest extends MainTest { 

    // more tests 
} 

class MethodTwoTest extends MainTest { 

    // more tests 
} 

但它應該有多次運行相同的測試不止一次。所以可以用某些東西來對它們進行參數化,這很合理。一些類實例:

import spock.lang.Specification 
abstract class MainSpecification extends Specification { 
    @Shared 
    protected Controller controller 

    def "test canary"() { 
     expect: 
     // do something with controller 
    } 

    // more tests 
} 

class MethodOneSpec extends MainSpecification { 
    def setupSpec() { 
     controller = //... first instance 
    } 

    // more tests 
} 

class MethodTwoSpec extends MainSpecification { 
    def setupSpec() { 
     controller = //... second instance 
    } 

    // more tests 
} 
+0

嘿,那工作!謝謝! – ArmYourselves 2013-04-06 11:43:31

相關問題