在Spock中,如何根据某些条件选择数据表中的某些行来运行?



是否有一种方法可以选择数据表中的某些行以根据其值运行?

例如,有时我希望运行所有包含a<5的行,有时我希望运行其余的行。

在测试中,表中可能有数百行数据。大多数时候我只想运行它的一个小子集。但是我不想只是复制这个方法,并将数据分成两个表。

class Math extends Specification {
def "maximum of two numbers"(int a, int b, int c) {
expect:
Math.max(a, b) == c
where:
a | b | c
1 | 3 | 3
7 | 4 | 4
0 | 0 | 0
}
}

我该怎么解决这个问题?

如果你使用Spock 2.0,那么你可以使用@Requires来过滤data变量。

class Example extends Specification {
@Requires({ a > 5})
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
a | b | c
1 | 3 | 3
7 | 4 | 7
0 | 0 | 0
}
}

结果

╷
└─ Spock ✔
└─ Example ✔
└─ maximum of two numbers ✔
├─ maximum of two numbers [a: 1, b: 3, c: 3, #0] ■ Ignored via @Requires
├─ maximum of two numbers [a: 7, b: 4, c: 7, #1] ✔
└─ maximum of two numbers [a: 0, b: 0, c: 0, #2] ■ Ignored via @Requires

有一件事要注意,这可能会改变在Spock 2.1到@Requires({ data.a > 5})这里是相关的问题。

相关内容

最新更新