Spock:可重用的数据表

Ale*_*tti 6 groovy spock

我可以像这样进行测试并将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"
}
Run Code Online (Sandbox Code Playgroud)

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

@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"        
}
Run Code Online (Sandbox Code Playgroud)

jih*_*hor 7

子句中的表格式数据where实际上在编译时解析为一组OR表达式,收集到列表列表中,然后转置,因此:

where:
a | b | c
1 | 0 | 1
2 | 2 | 2
4 | 5 | 5
Run Code Online (Sandbox Code Playgroud)

将转变为这样:

where:
a << [1, 2, 4]
b << [0, 2, 5]
c << [1, 2, 5]
Run Code Online (Sandbox Code Playgroud)

在生成测试方法之前(请参阅org.spockframework.compiler.WhereBlockRewriter详细信息)。此构造在文档var << list中称为“数据管道” 。

稍微升级一下现有的答案,从 Spock 1.1 开始,可以使用一种名为“多变量数据管道”的结构来稍微缩短代码,该结构将转置表格:

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

    static def dataTable() {
        [
                [1, 0, 1],
                [2, 2, 2],
                [4, 5, 5]
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

有趣的事实:虽然语法变体的文档没有解释原因,但这是因为表行被解析为一组OR表达式,双杠也可以使用 -

where:
a | b || c
1 | 0 || 1
2 | 2 || 2
4 | 5 || 5
Run Code Online (Sandbox Code Playgroud)


Mar*_*nik 5

是的你可以。

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 ,2 ,5]
 }

 private List<Integer> cProvider() {
    [1 ,2 ,5]
 }
}
Run Code Online (Sandbox Code Playgroud)

当然,aProvider/bProvider/cProvider 可以以“更常规的方式”重写,除其他外,可以将其外部化为某个类并在许多测试中重用。您不必指定表,但可以提供“数据管道”。请阅读数据驱动测试章节了解更多内容。