我在Spock中了解data driven testing 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子句。我可以通过在测试中读取环境变量并使用不同的环境变量多次运行测试套件来实现这一点(测试矩阵样式作为CI服务,如travis支持它)。
但我更愿意直接在测试中这样做,所以我不必多次运行测试套件。 Spock是否以某种方式支持这个?
答案 0 :(得分:4)
可能不是100%可能,但您可以将右侧放在方法中并使用@Shared
进行注释。这将允许您从每个测试中提取该逻辑。
例如:
myTest () {
@Shared combinations = getCombinations()
someTest() {
...
where:
[firstlibVersion, secondLibVersion] << combinations
}
def getCombinations() {
[['0.1', '0.2'], ['0.2', '0.4']].combinations()
}