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?
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:
@SuiteClasses
no longer work? –
Amphitropous @SuiteClasses
might work for the OP. Thx! –
Cavorelievo © 2022 - 2024 — McMap. All rights reserved.
Specification
s? Or multiple tests in aSpecification
? – Absentee@Stepwise
in action because it tested a long page flow including external systems hosted by partners. And BTW, we still felt bad about@Stepwise
. – Luz@AfterTest
. The spec order has been answered below. "I noticed TestNG has got @AfterTest annotation, so does Spock have something similar?" - You probably want@AfterClass
. An alternative is to have a method namedcleanupSpec()
in your spec. – Amphitropous