How to tell Spock which class to run in specific order
Asked Answered
D

1

5

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?

Donegal answered 26/2, 2018 at 10:17 Comment(4)
You mean you have multiple Specifications? Or multiple tests in a Specification?Absentee
I have multiple classes that extends SpecificationDonegal
If your test suite depends on the order of specs and specs depend on the order of feature methods (FM), your test suite is broken. Stay away from that. A spec is to be independent of other specs, a FM is to be independent of other FMs in the same spec. I am an agile coach and in one of my last projects there was a massive Spock/Geb test suite with thousands of tests, and in exactly one very complex Geb integration test we had @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
You have 2 separate questions. One has to do with spec order and the other has to do with @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 named cleanupSpec() in your spec.Amphitropous
C
6

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

Cavorelievo answered 26/2, 2018 at 10:25 Comment(4)
thanks @Szymon, I already use Stepwise. it runs tests in order that I want but it doesn't run the classes in the order that I want, so I'm afraid this is not what I need. sorry for not being clear.Donegal
@Donegal This is the only ordered execution you can achieve with Spock. You cannot specify the order of test classes (specifications) as I mentioned in the first paragraph. And basically you shouldn't design your tests to run in specific order - each unit test runs in isolation and it should not depend on side effects from other tests.Cavorelievo
@SzymonStepniak "This is the only ordered execution you can achieve with Spock." - Does a Suite marked with @SuiteClasses no longer work?Amphitropous
@JeffScottBrown Thanks Jeff for the comment. You're right, using @SuiteClasses might work for the OP. Thx!Cavorelievo

© 2022 - 2024 — McMap. All rights reserved.