Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow spec iterations to be skipped #1008

Open
renatoathaydes opened this issue Jul 5, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@renatoathaydes
Copy link

commented Jul 5, 2019

Feature request

Spock does not currently support natively skipping a single iteration of an examples-based feature. The workaround mentioned in the documentation involves using JUnit's AssumptionViolatedException to handle such cases.

Spock already correctly reports such iterations as "skipped" to the JUnit Notifier, but not to Spock's own IRunListener (which, incidentally, causes a bug in spock-reports as it cannot know that a particular iteration of a feature was skipped).

It would be ideal to be able to use a Spock, backend-test-framework-independent way of doing this. Specifically, Spock's IRunListener should have a method, void iterationSkipped( IterationInfo iteration ), analogous to the existing void specSkipped( SpecInfo spec ).

Proposed solution 1

I would suggest adding a method to Specification like void skipIf(boolean) and/or assumeThat(boolean), that causes the current feature/iteration to be skipped when necessary.

Proposed solution 2

It might be more Spock-like to use a precondition block instead:

precondition: 'must be on Windows'
OS.isWindows()

Example

Here's a minimal specification demonstrating how feature iterations can be skipped today, but only when using JUnit APIs.

import org.junit.Assume
import spock.lang.Specification
import spock.lang.Unroll

class AssumptionViolatedTest extends Specification {
    @Unroll
    def "Can ignore test if AssumptionViolationException is thrown"() {
        setup: 'check some precondition'
        Assume.assumeFalse( i == 2 )
        expect:
        i > 0
        where:
        i << [ 1, 2, 3 ]
    }
}

With the first proposed solution, it would look like this:

import spock.lang.Specification
import spock.lang.Unroll

class AssumptionViolatedTest extends Specification {
    @Unroll
    def "Can ignore test if precondition is not satisfied"() {
        setup: 'check some precondition'
        assumeThat( i == 2 )

        expect:
        i > 0
        where:
        i << [ 1, 2, 3 ]
    }
}

With the second proposal:

import spock.lang.Specification
import spock.lang.Unroll

class AssumptionViolatedTest extends Specification {
    @Unroll
    def "Can ignore test if precondition is not satisfied"() {
        precondition: 'check some precondition'
        i == 2

        expect:
        i > 0
        where:
        i << [ 1, 2, 3 ]
    }
}

Thank you for providing the community with a best-in-class testing framework, and hope that this small addition can help make Spock become even better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.