How do I programmatically skip a test in the Spock framework? I know I can annotate a test with @Ignore
to skip it, or use @IgnoreIf
to skip tests based on environmental variables and the like. But is there a way to run arbitrary code that decides whether or not a test should run?
For example, let's say I have an integration test that has to connect to a third-party service's sandbox environment. Outages in the service's sandbox environment cause the test to fail. Assuming I've written a method canConnectToService
that checks if the test will be able to connect to this service, how do I write a test that will be skipped if canConnectToService()
returns false?
By default, Spock runs tests sequentially with a single thread. As of version 2.0, Spock supports parallel execution based on the JUnit Platform. To enable parallel execution set the runner.
Use JUnit's Assume class. Specifically, you could write Assume.assumeTrue(canConnectToService())
at the beginning of your test to skip the test if the third party service is unavailable. This method will throw an AssumptionViolatedException
if canConnectToService()
returns false, and Spock ignores tests that are interrupted by an AssumptionViolatedException
for JUnit compatibility (see this bug report).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With