首页 > 解决方案 > 在 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
    }
}

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

标签: groovyspock

解决方案


如果您使用 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})为相关问题


推荐阅读