在 Spock 中,如何根据某些条件 select 数据 table 到 运行 中的某些行?

In Spock, how to select certain rows in data table to run, based on some condition?

有没有办法根据其值 select 数据 table 到 运行 中的某些行?

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

在测试中碰巧table中可以有数百行数据。大多数时候我只想 运行 它的一小部分。但我不想只是复制这个方法,并将数据分成两个 tables.

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}) 这里是相关的 issue