2013-04-06 54 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

相关问题