Spock: reusable data tables

Is it possible to run such a test and extract the where where data table into a reusable block?

@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"
}

something like this (pseudo code):

@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"        
}
+4
source share
3 answers

Yes, you can.

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]
 }
}

Of course, aProvider / bProvider / cProvider can be rewritten in the "groovier way" and, among other things, can be inferred from the outer class to some class and reused in many tests. You do not need to specify a table, but can supply "data channels". Read more in Chapter 3.

+1
source

where OR , , :

where:
a | b | c
1 | 0 | 1
2 | 2 | 2
4 | 5 | 5

:

where:
a << [1, 2, 4]
b << [0, 2, 5]
c << [1, 2, 5]

, ( . org.spockframework.compiler.WhereBlockRewriter). var << list " " .

, Spock 1.1, , "Multi-Variable Data Pipes", :

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]
        ]
    }
}

: docs , , OR, -

where:
a | b || c
1 | 0 || 1
2 | 2 || 2
4 | 5 || 5
+1

, , ! , , .

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:
            params << dataTable()
            a = params.a
            b = params.b
            c = params.c
    }

    // this is now reusable in multiple tests
    def dataTable() {
        return [
            // case 1
            [
                a: 1,
                b: 0,
                c: 1
            ],

            // case 2
            [
                a: 1,
                b: 2,
                c: 2
            ],

            // case 3
            [
                a: 4,
                b: 5,
                c: 5
            ]
        ]
    }
}
0

Source: https://habr.com/ru/post/1687231/


All Articles