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

use*_*625 3 groovy 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
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以尝试什么来解决这个问题?

Leo*_*ngs 7

如果您使用 Spock 2.0,那么您可以使用变量@Requires进行过滤data

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

结果

\n
\xe2\x95\xb7\n\xe2\x94\x94\xe2\x94\x80 Spock \xe2\x9c\x94\n   \xe2\x94\x94\xe2\x94\x80 Example \xe2\x9c\x94\n      \xe2\x94\x94\xe2\x94\x80 maximum of two numbers \xe2\x9c\x94\n         \xe2\x94\x9c\xe2\x94\x80 maximum of two numbers [a: 1, b: 3, c: 3, #0] \xe2\x96\xa0 Ignored via @Requires\n         \xe2\x94\x9c\xe2\x94\x80 maximum of two numbers [a: 7, b: 4, c: 7, #1] \xe2\x9c\x94\n         \xe2\x94\x94\xe2\x94\x80 maximum of two numbers [a: 0, b: 0, c: 0, #2] \xe2\x96\xa0 Ignored via @Requires\n
Run Code Online (Sandbox Code Playgroud)\n

需要注意的一件事是,这可能会在 Spock 2.1 中更改为@Requires({ data.a > 5})相关问题

\n