S1lentSt0rm
S1lentSt0rm

Reputation: 2039

Same `where` clause for multiple tests

I know about data driven testing with the where clause in Spock. But how can I expand this to use one where for multiple tests?

For example, I have I set of tests I want to run against different versions of a library:

@Unroll
def "test1 #firstlibVersion, #secondLibVersion"() {...}

@Unroll
def "test2 #firstlibVersion, #secondLibVersion"() {...}
...

The where clause might look like this:

where:
[firstlibVersion, secondLibVersion] <<
    [['0.1', '0.2'], ['0.2', '0.4']].combinations()

I don't want to repeat this same where clause in every test. I could achieve that by reading environment variables in the tests and running the test suite multiple times with different env vars (test matrix style as CI services like travis support it).

But I would prefer to do that directly in the tests so I don't have to run the test suite multiple times. Does Spock support this somehow?

Upvotes: 2

Views: 191

Answers (1)

Destruktor
Destruktor

Reputation: 463

Might not be 100% possible, but you can put the right hand side in a method and annotate with @Shared. This would allow you to extract that piece of logic from each test.

For example:

myTest () {
    @Shared combinations = getCombinations()

    someTest() {

        ...
        where:
            [firstlibVersion, secondLibVersion] << combinations

    }

    def getCombinations() {
        [['0.1', '0.2'], ['0.2', '0.4']].combinations()
    }

Upvotes: 5

Related Questions