Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Spock which class to run in specific order

Tags:

groovy

spock

Im using Spock for my tests and I have multiple classes to test. I want to tell Spock to test each class in specific order. Is there a way to do this? I noticed TestNG has got @AfterTest annotation, so does Spock have something similar?

like image 928
groovierthangroovy Avatar asked Feb 26 '18 10:02

groovierthangroovy


1 Answers

You can't specify Spock test classes execution order. However Spock allows you to specify methods execution order when you add @Stepwise annotation to your test class - in this case Spock will execute all methods in order from top to bottom and if one method fails it will ignore remaining methods in this test class.

Indicates that a spec's feature methods should be run sequentially in their declared order (even in the presence of a parallel spec runner), always starting from the first method. If a method fails, the remaining methods will be skipped. Feature methods declared in super- and subspecs are not affected.

@Stepwise is useful for specs with (logical) dependencies between methods. In particular, it helps to avoid consecutive errors after a method has failed, which makes it easier to understand what really went wrong.

Reference: http://spockframework.org/spock/javadoc/1.1/spock/lang/Stepwise.html

@Stepwise
class StepwiseExample extends Specification {

    def "first test method to run"() {
        // ....
    }
    
    def "second test method to run"() {
        // ....
    }
    
    def "if previous method failed this one will be ignored"() {
        // ....
    }
}

Using org.junit.runners.Suite

Jeff Scott Brown gave a good comment about JUnit's @Suite.SuiteClasses. In this case you create a class where you can aggregate your test classes (specifications) into a single test suite and those classes will be executed in the order they have been defined in the suite. Consider following example:

import org.junit.runner.RunWith
import org.junit.runners.Suite

@RunWith(Suite)
@Suite.SuiteClasses([
        Test2Specification,
        Test1Specification
])
class TestSuiteSpecification { }

This suite executes two specifications Test2Specification and Test1Specification in the defined order:

enter image description here

like image 177
Szymon Stepniak Avatar answered Oct 19 '22 11:10

Szymon Stepniak