zoran119
zoran119

Reputation: 11307

Run test setup only once

Given a spock test below, the setup block runs once for each data element in where block. Can I make it run only once?

setup:
def x = 1

when:
x++

then:
x == y

where:
y << [2, 3, 4]

Upvotes: 4

Views: 4070

Answers (1)

topr
topr

Reputation: 4612

Just use @Shared annotation and declare x as class field. The value is going to be reused between feature method executions (between multiple feature methods either).

class SomeSpockSpec extends Specification {

    @Shared def x = 1

    def 'x going to be incremented'() {
            when:
            x++

            then:
            x == y

            where:
            y << [2, 3, 4]
    }
}

Upvotes: 2

Related Questions