gpt4 book ai didi

groovy - 仅运行测试设置一次

转载 作者:行者123 更新时间:2023-12-02 03:42:00 27 4
gpt4 key购买 nike

给定下面的spock测试,setup块针对where块中的每个数据元素运行一次。我可以只运行一次吗?

setup:
def x = 1

when:
x++

then:
x == y

where:
y << [2, 3, 4]

最佳答案

只需使用@Shared批注并将x声明为类字段即可。该值将在特征方法执行之间重用(或者在多个特征方法之间重用)。

class SomeSpockSpec extends Specification {

@Shared def x = 1

def 'x going to be incremented'() {
when:
x++

then:
x == y

where:
y << [2, 3, 4]
}
}

关于groovy - 仅运行测试设置一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19533152/

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