Reputation: 83
I'm currently trying to run the same test cases for 2 different classes but having issues with the setup(), I see similar questions, but haven't seen the solution for groovy testing with Spock, and I haven't been able to figure it out.
So I am essentially solving the same problem using 2 different methods, so the same test cases should be applicable to both classes, I am trying to stay Don't Repeat Yourself (DRY).
So I've set up a MainTest as an abstract class and the MethodOneTest and MethodTwoTest as concrete classes that extend the 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 are something like this:
class MethodOneTest extends MainTest {
def setup() {
def controller = new MethodOneTest()
}
}
class MethodTwoTest extends MainTest {
def setup() {
def controller = new MethoTwoTest()
}
}
So does anyone know how I can do run all the tests in abstract MainTest from my concrete classes MethodOneTest and MethodTwoTest? How to instantiate the setup properly? I hope I am being clear.
Upvotes: 0
Views: 2692
Reputation: 3791
Just forget about controller setup. All tests from superclass will be automatically executed when you execute tests for 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 it should have sence to run the same tests more than once. So it is resonable to parameterize them with something, e.g. some class instance:
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
}
Upvotes: 3