How can Spock be made to retry failed Geb tests?

geb, grails, groovy, spock

Solution

I know this question is a year old, but we've been having the same problem. Following Peter's suggestion, I created a Spock extension (https://github.com/anotherchrisberry/spock-retry).

If you have a base specification (which was our case), you can just add a `@RetryOnFailure` annotation to it:

@RetryOnFailure
class BaseFunctionalSpec extends Specification {
    //    all tests will execute up to two times before failing
}

Or, you can add it to a specific feature:

class SomeSpec extends Specification {

    @RetryOnFailure(times=3)
    void 'test something that fails sporadically'() {
        // will execute test up to three times before failing
    }
}

Problem

I have functional tests for a Grails app which use Geb and Spock. Occasionally, a functional test will fail for timeouts or other sporadic behavior. In previous projects using TestNG, I'd have a retryAnalyzer just trigger a retry during test execution to see if it fails both times (and then fail for real). How do I get Spock to retry a failed test?

Original source

Related problems