使用 Spock 的 Unroll 进行所有可能的参数排列

问题描述 投票:0回答:2

我有以下参数用于相同的测试:

  a  |  b  |  c
  1  |  2  |  3
 11  | 22  | 33

Spock 为与此类似的测试提供

@Unroll
注释(使用这组参数,您可以使用向量 [1, 2, 3] 和 [11, 22, 33] 运行相同的测试)。

但是,我需要对所有可能的排列(例如 [1, 2, 3]、[1, 2, 33]、[11, 2, 33] 等,所有 8 种组合)运行相同的测试。我怎样才能实现它?

感谢您的任何想法!

groovy integration-testing spock end-to-end
2个回答
26
投票

你需要

where:
[a, b, c] << [[1, 11], [2, 12], [3, 13]].combinations()

0
投票

回答cellepo的评论: 通过提取

c
数据管道,可以得到与排列相关的期望。

  def "data driven cross-product"() {
    expect:
    Math.max(a, b) == c

    where:
    [a, b] << [[1, 11], [2, 12]].combinations()
    c << [2, 11, 12, 12]
  }

combinations
始终以相同的顺序返回排列:

[['a', 'b'],[1, 2, 3]].combinations() == [['a', 1], ['b', 1], ['a', 2], ['b', 2], ['a', 3], ['b', 3]]

如果是很多变量的组合,那就很难写了。

© www.soinside.com 2019 - 2024. All rights reserved.