我知道data driven testing与 Spock 中的 where
子句一起使用。
但是我如何扩展它以使用一个 where
进行多个测试?
例如,我有一组想要针对不同版本的库运行的测试:
@Unroll
def "test1 #firstlibVersion, #secondLibVersion"() {...}
@Unroll
def "test2 #firstlibVersion, #secondLibVersion"() {...}
...
where 子句可能如下所示:
where:
[firstlibVersion, secondLibVersion] <<
[['0.1', '0.2'], ['0.2', '0.4']].combinations()
我不想在每个测试中重复相同的 where 子句。我可以通过读取测试中的环境变量并使用不同的环境变量多次运行测试套件来实现这一点(测试矩阵样式,因为 travis 等 CI 服务支持它)。
但我更愿意直接在测试中执行此操作,这样我就不必多次运行测试套件。 Spock 以某种方式支持这一点吗?
最佳答案
可能不是 100% 可能,但您可以将右侧放在方法中并使用 @Shared
进行注释。这将允许您从每个测试中提取该逻辑。
例如:
myTest () {
@Shared combinations = getCombinations()
someTest() {
...
where:
[firstlibVersion, secondLibVersion] << combinations
}
def getCombinations() {
[['0.1', '0.2'], ['0.2', '0.4']].combinations()
}
关于testing - 多个测试的相同 `where` 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156262/