Spock:循环中的参数化测试

时间:2016-09-05 10:06:00

标签: groovy junit spock

在JUnit中,可以在循环中执行多个测试用例: @RunWith(Parameterized.class)

如何在Spock中实现这一目标?

1 个答案:

答案 0 :(得分:0)

如果您希望针对相同的数据集运行多个测试,您可以执行以下操作:

@Shared
def numOnes = [1, 2]
@Shared
def numTwos = [1, 3]

@Unroll("When I add #numOne and #numTwo then the response is #resultNum")
def "Simple addition test"() {
    when:
    int response = numOne + numTwo
    then:
    response == resultNum
    where:
    numOne << numOnes
    numTwo << numTwos
    resultNum << [2, 5]
}

@Unroll("When I take #numOne away from #numTwo then the response is #resultNum")
def "Simple subtraction test"() {
    when:
    int response = numTwo - numOne
    then:
    response == resultNum
    where:
    numOne << numOnes
    numTwo << numTwos
    resultNum << [0, 1]
}

注意只能从@Shared块访问where和静态字段。共享只是声明该文件中的所有defs都可以访问该字段。