用于多个测试的相同“where”子句

S1l*_*0rm 2 testing groovy spock

我了解Spock 中的子句数据驱动测试where。但我如何扩展它以使用一个where进行多个测试呢?

例如,我有一组想要针对不同版本的库运行的测试:

@Unroll
def "test1 #firstlibVersion, #secondLibVersion"() {...}

@Unroll
def "test2 #firstlibVersion, #secondLibVersion"() {...}
...
Run Code Online (Sandbox Code Playgroud)

where 子句可能如下所示:

where:
[firstlibVersion, secondLibVersion] <<
    [['0.1', '0.2'], ['0.2', '0.4']].combinations()
Run Code Online (Sandbox Code Playgroud)

我不想在每个测试中重复同样的 where 子句。我可以通过读取测试中的环境变量并使用不同的环境变量多次运行测试套件来实现这一点(测试矩阵样式,因为 travis 等 CI 服务支持它)。

但我更愿意直接在测试中执行此操作,这样我就不必多次运行测试套件。Spock 以某种方式支持这一点吗?

Des*_*tor 5

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

例如:

myTest () {
    @Shared combinations = getCombinations()

    someTest() {

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

    }

    def getCombinations() {
        [['0.1', '0.2'], ['0.2', '0.4']].combinations()
    }
Run Code Online (Sandbox Code Playgroud)