Spock Stepwise

In some situations it would be useful to run certain tests only if some other more basic tests succeed. Not because they depend on data the basic tests produce (which would be a bad practice) but because they use functionality which is tested by the basic test. TestNG for example supports this with it’s dependsOnMethods attribute:

Let’s take the example of a multiply(a, b) routine and a square(a) routine. Internally square uses multiply and therefore it does not make sense to run the test for square if the test for multiply does not succeed. In Spock you can use the @Stepwise annotation on the class to define that the tests should run sequentially in the order they are declared. If a test fails the rest of the tests are skipped. This is not as flexible as TestNG but should be good enough in most situations.

import spock.lang.*

class StepwiseSpock extends Specification {
 def "verify that 'multiply' works"() {
 def a = 2
 def b = 3
 def c = multiply(a, b)
 c == 6
 def "verify that 'square' works"() {
 def a = 3
 def c = square(a)
 c == 9
 def multiply(a, b) {
 a + b
 def square(a) {
 multiply(a, a)

Without @Stepwise both tests fail as expected:

With @Stepwise the test for multiply fails and the test for square is skipped:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s