多个测试的`where`子句相同

时间:2016-02-02 14:35:55

标签: testing groovy spock

我在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是否以某种方式支持这个?

1 个答案:

答案 0 :(得分:4)

可能不是100%可能,但您可以将右侧放在方法中并使用@Shared进行注释。这将允许您从每个测试中提取该逻辑。

例如:

myTest () {
    @Shared combinations = getCombinations()

    someTest() {

        ...
        where:
            [firstlibVersion, secondLibVersion] << combinations

    }

    def getCombinations() {
        [['0.1', '0.2'], ['0.2', '0.4']].combinations()
    }