Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically skip a test in Spock

Tags:

spock

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?

like image 921
fzzfzzfzz Avatar asked Nov 20 '15 04:11

fzzfzzfzz


People also ask

Does Spock run tests in parallel?

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.


1 Answers

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).

like image 108
fzzfzzfzz Avatar answered Sep 30 '22 05:09

fzzfzzfzz