加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy – Spock:可重用的数据表

发布时间:2020-12-14 16:24:20 所属栏目:大数据 来源:网络整理
导读:我可以像这样进行测试并将where子句数据表提取到可重用的块中吗? @Unrollvoid "test that doSomething with #a and #b does not fail"(String a,String b) { when: doSomethingWithAandB(a,b) then: notThrown(Exception) where: a | b "foo" | "bar" "foo"
我可以像这样进行测试并将where子句数据表提取到可重用的块中吗?

@Unroll
void "test that doSomething with #a and #b does not fail"(String a,String b) {
    when:
        doSomethingWithAandB(a,b)
    then:
        notThrown(Exception)
    where:
        a     | b
        "foo" | "bar"
        "foo" | "baz"
        "foo" | "foo"
}

像这样的东西(伪代码):

@Unroll
void "test that doSomethingElse with #a and #b does not fail"(String a,String b) {
    when:
        doSomethingElseWithAandB(a,b)
    then:
        notThrown(Exception)
    where:
        dataTable()
}

def dataTable(a,b) {  // this is now reusable in multiple tests
        a     | b
        "foo" | "bar"
        "foo" | "baz"
        "foo" | "foo"        
}

解决方法

是的你可以.

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

class SampleTest extends Specification {
  @Unroll
  def "max of #a and #b gives #c"() {
  expect:
    Math.max(a,b) == c
  where:
    a << aProvider()
    b << bProvider()
    c << cProvider()
 }

 private List<Integer> aProvider() {
   [1,2,4]
 }
 private List<Integer> bProvider() {
   [0,5]
 }

 private List<Integer> cProvider() {
    [1,5]
 }
}

当然,aProvider / bProvider / cProvider可以以“更加通用的方式”重写,除其他外,可以外部化到某个类并在许多测试中重用.您不必指定表,但可以提供“数据管道”.在Data Driven Testing章中阅读更多内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读