3

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?

John Flinchbaugh
  • 2,338
  • 1
  • 17
  • 20

2 Answers2

9

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
    }
}
Chris B
  • 598
  • 4
  • 6
2

You'd have to write a little JUnit rule (for example something like https://gist.github.com/897229) or Spock extension. You'd probably have to live with some limitations like the same spec instance being reused and JUnit just reporting a single test, but hopefully nothing that rules out the approach altogether. (One thing that comes to mind is that mocking might not work.) In a future version of Spock, repeating a test (or its building blocks) will likely become a first-class concept, doing away with these limitations.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • I've finally gone down the junit rule route, and added a class similar to the one here: http://stackoverflow.com/questions/8295100/how-to-re-run-failed-junit-tests-immediately . I put println statements around the base.evaluate() call, and I can tell my rule is being run, but I'm finding that it always *returns*, and I never see a Throwable thrown, even on failure. Is there another way to tell the test failed? – John Flinchbaugh Oct 19 '12 at 17:31