unit testing - running same tests for different classes in groovy and spock -


i'm trying run same test cases 2 different classes having issues setup(), see similar questions, haven't seen solution groovy testing spock, , haven't been able figure out.

so solving same problem using 2 different methods, same test cases should applicable both classes, trying stay don't repeat (dry).

so i've set maintest abstract class , methodonetest , methodtwotest concrete classes extend abstract 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 } 

my concrete classes this:

class methodonetest extends maintest {     def setup() {         def controller = new methodonetest()     } }  class methodtwotest extends maintest {     def setup() {         def controller = new methotwotest()     } } 

so know how can run tests in abstract maintest concrete classes methodonetest , methodtwotest? how instantiate setup properly? hope being clear.

just forget controller setup. tests superclass automatically executed when execute tests concrete class. e.g.

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 } 

but should have sence run same tests more once. resonable parameterize them something, e.g. class instance:

import spock.lang.specification abstract class mainspecification extends specification {     @shared      protected controller controller      def "test canary"() {         expect:         // 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 } 

Comments