我目前正在尝试为 2 个不同的类运行相同的测试用例,但是 setup() 有问题,我看到了类似的问题,但还没有看到使用 Spock 进行常规测试的解决方案,而且我无法想办法。
所以我本质上是使用 2 种不同的方法来解决相同的问题,所以相同的测试用例应该适用于两个类,我试图保持不要重复自己(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 中的所有测试?如何正确实例化设置?我希望我很清楚。