Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repeat Spock test?

Tags:

grails

spock

As described here @Repeat annotation is not supported right now. How can I mark spock test as repeated n times?

Suppose I have spock test:

def "testing somthing"() {
    expect:
    assert myService.getResult(x) == y

    where:
    x | y
    5 | 7
    10 | 12
}

How can I mark it to repeat n times?

like image 279
fedor.belov Avatar asked Jun 06 '12 07:06

fedor.belov


2 Answers

You can use @Unroll annotation like this:

@Unroll("test repeated #i time")
def "test repeated"() {
    expect:
        println i
    where:
        i << (1..10)
}

It will create 10 separate tests for you.

EDIT after you've edited your question, use the simplest way to achieve this:

def "testing somthing"() {
    expect:
        assert myService.getResult(x) == y

    where:
        x | y
        5 | 7
        5 | 7
        5 | 7
        5 | 7
        5 | 7
        10 | 12
        10 | 12
        10 | 12
        10 | 12
        10 | 12

}

This is currently only way to do this in spock.

like image 139
Tomasz Kalkosiński Avatar answered Nov 01 '22 17:11

Tomasz Kalkosiński


You can use a where-block as shown in the answer above. There is currently no way to repeat a method that already has a where-block.

like image 29
Peter Niederwieser Avatar answered Nov 01 '22 17:11

Peter Niederwieser