如何忽略或跳过特定案例?

huangapple go评论85阅读模式
英文:

Spock: how to ignore or skip certian case?

问题

int[] randomOnCondition(int input) {
    def output = input == 1 ? Random.newInstance().nextInt() : input
    [input, output]
}

def test() {
    expect:
    randomOnCondition(input) == output as int[]

    where:
    input || output
    2     || [2, 2]
    1     || [1, _]   //如何匹配结果的一部分并忽略其他部分?
}

updated

def test() {
    expect:
    Integer[] output = randomOnCondition(input)
    output[0] == output0
    output[1] == output1

    where:
    input || output0 | output1
    2     || 2       | 2
    1     || 1       | _  //对于某些情况,可以跳过断言吗?
}

看起来没有解决方案,我应该将用例拆分成两个。


<details>
<summary>英文:</summary>

With Spock, assume some function that will return unexpectable result on certian condition, how to match part of the result and ignore others?
  

int[] randomOnCondition(int input) {
def output = input == 1 ? Random.newInstance().nextInt() : input
[input, output]
}

def test() {
expect:
randomOnCondition(input) == output as int[]

where:
input || output
2     || [2, 2]
1     || [1, _]   //how to match part of the result and ignore others?

}


updated

def test() {
expect:
Integer[] output = randomOnCondition(input)
output[0] == output0
output[1] == output1

where:
input || output0 | output1
2     || 2       | 2
1     || 1       | _  //for somecase , can it skip assert?

}

Seems no solution, I should split the case into two

</details>


# 答案1
**得分**: 1

这是我在之前评论中解释的示例:

```lang-groovy
package de.scrum_master.stackoverflow.q63355662

import spock.lang.Specification
import spock.lang.Unroll

class SeparateCasesTest extends Specification {
  int[] randomOnCondition(int input) {
    def output = input % 2 ? Random.newInstance().nextInt() : input
    [input, output]
  }

  @Unroll
  def "对于输入 #input 的可预测输出"() {
    expect:
    randomOnCondition(input) == output

    where:
    input || output
    2     || [2, 2]
    4     || [4, 4]
    6     || [6, 6]
  }

  @Unroll
  def "对于输入 #input 的部分不可预测输出"() {
    expect:
    randomOnCondition(input)[0] == firstOutputElement

    where:
    input || firstOutputElement
    1     || 1
    3     || 3
    5     || 5
  }
}

更新: 与您的问题略不相关,但如果输出确实包含输入值,可以简化您的测试如下:

  @Unroll
  def "对于输入 #input 的可预测输出"() {
    expect:
    randomOnCondition(input) == [input, input]

    where:
    input << [2, 4, 6]
  }

  @Unroll
  def "对于输入 #input 的部分不可预测输出"() {
    expect:
    randomOnCondition(input)[0] == input

    where:
    input << [1, 3, 5]
  }
英文:

Here is an example of what I explained in my previous comment:

package de.scrum_master.stackoverflow.q63355662

import spock.lang.Specification
import spock.lang.Unroll

class SeparateCasesTest extends Specification {
  int[] randomOnCondition(int input) {
    def output = input % 2 ? Random.newInstance().nextInt() : input
    [input, output]
  }

  @Unroll
  def &quot;predictable output for input #input&quot;() {
    expect:
    randomOnCondition(input) == output

    where:
    input || output
    2     || [2, 2]
    4     || [4, 4]
    6     || [6, 6]
  }

  @Unroll
  def &quot;partly unpredictable output for input #input&quot;() {
    expect:
    randomOnCondition(input)[0] == firstOutputElement

    where:
    input || firstOutputElement
    1     || 1
    3     || 3
    5     || 5
  }
}

Update: Somewhat unrelated to your question, but a way to streamline your test if the output really contains input values:

  @Unroll
  def &quot;predictable output for input #input&quot;() {
    expect:
    randomOnCondition(input) == [input, input]

    where:
    input &lt;&lt; [2, 4, 6]
  }

  @Unroll
  def &quot;partly unpredictable output for input #input&quot;() {
    expect:
    randomOnCondition(input)[0] == input

    where:
    input &lt;&lt; [1, 3, 5]
  }

huangapple
  • 本文由 发表于 2020年8月11日 17:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63355662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定