gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:20:10 25 4
gpt4 key购买 nike

我知道 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 子句。我可以通过读取测试中的环境变量并使用不同的环境变量多次运行测试套件来实现这一点(测试矩阵样式,因为 CI 服务如 travis 支持它)。

但我更愿意直接在测试中这样做,这样我就不必多次运行测试套件。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com