有没有办法在Spock上测试索引? 我有一个像这样的地方块:
where:
column1 | column2
1 | 3
1 | 4
2 | 5
6 | 8
我想知道是否可以在我的测试中执行索引。 因此,如果我正在运行第一个测试(1 | 3),那么我的索引将为0。 如果我正在运行第三个测试(2 | 5),我的索引将是2。
有没有办法让我的测试中有这个索引?
答案 0 :(得分:1)
您需要在测试定义中添加@Unroll
,然后在测试名称中添加#column1
以输出值
@Unroll
def "something #column1 and #column2"() {
...
}
答案 1 :(得分:1)
简单的答案是添加一个索引或案例或一些可以在结果部分中检查的分类变量。
where:
idx | column1 | column2
0 | 1 | 3
1 | 1 | 4
2 | 2 | 5
3 | 6 | 8
但我不得不怀疑是否使用where子句来运行可能应该是单个测试的多个测试用例。
idx不应告诉测试代码在expect / then块中执行哪些检查,并且不应该驱动任何正在测试的代码。
如果输入的顺序为:
,则测试会产生不同的结果where:
idx | column1 | column2
0 | 6 | 8
1 | 2 | 5
2 | 1 | 4
3 | 1 | 3
然后我认为测试需要被分解,因为顺序敏感性似乎表明该测试正在测试除了column1和column2值对之外的其他东西,并且使用的地方并不恰当。< / p>
答案 2 :(得分:0)
如下所示就足够了吗?
import spock.lang.*
@Unroll
class MyFirstSpec extends Specification {
def "column1 is #column1 and column2 is #column2 where index is #index"() {
expect:
if(index in [0, 1]) {
assert column1 < column2
} else {
assert column1 == column2
}
where:
[column1, column2, index] << [
[1, 2, 4, 5], [2, 3, 4, 5]
].transpose().withIndex()*.flatten()
}
}
运行此Sample
注意:
withIndex()
可从Groovy 2.4.0获得
答案 3 :(得分:0)
添加索引列可以采用常规方法。只需将以下行添加到“ where:”块的末尾。该列表应与数据行中的项目数相同
idx << [0,1,2,3]
或
idx << (0..3).collect()
参考:http://spockframework.org/spock/docs/1.0/data_driven_testing.html
的Secton“组合数据表,数据管道和变量分配”答案 4 :(得分:0)
有一个默认值(现在?):#iterationCount
,请参阅here。您可以使用您的方法名称来使用它,例如
@Unroll
def "testing entry #iterationCount from where block"() {
// ...
}
顺便说一下,还有#featureName
可用。