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
}
}
我可以尝试什么来解决这个问题?
解决方案
如果您使用 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})
为相关问题。
推荐阅读
- awk - 合并文件,其中一列的值位于另一个文件中两列的值之间
- vue.js - 关于 IIS 别名问题的 Vue SPA
- asp.net-core - 操作方法“SaveForm”上的 ASP.Net 保存表单导致 InvalidOperationException:未找到视图“SaveForm”
- python - 如何使循环运行特定次数
- python-3.x - 使用 Python3 从客户端 JS 中抓取数据
- c# - GC.Collect 后 C# 引用对象仍然存在
- pandas - 在 excel 列中搜索值,记录在变量中找到的行
- javascript - 在jQuery中删除没有文本的元素并分离带有文本的子元素
- python - 使用 Python 存储 Google Auth 凭据
- c# - 如何使聚合根更小