Reputation: 847
I have been writing some tests using Groovy/Spock and came to the need of some decorator/logic that would only run some tests based in a variable's value
I was reading about @IgnoreIf which is the perfect option as it allows me to write something in the lines of
@IgnoreIf(value = { !enabled }, reason = "Feature flag not enabled")
The problem I have is, the reason argument only got released in 2.1 and my company is using 1.3 due to some major issues whilst migrating to v2.
Is there any other option I could use to achieve the same result, bearing in mind that, at the end of they day, I want to see skipped tests in the pipeline but with a reason why.
Thanks
Upvotes: 0
Views: 383
Reputation: 67287
The problem I have is, the reason argument only got released in 2.1 and my company is using 1.3 due to some major issues whilst migrating to v2.
My recommendation is to fix those issues and upgrade instead of investing a lot of work to add a feature to a legacy Spock version. Solve the problem instead of evading it.
I want to see skipped tests in the pipeline but with a reason why.
You did not specify what exactly you need. Is this about console log or rather about generated test reports? In the former case, a cheap trick would be:
@IgnoreIf({ println 'reason: back-end server unavailable on Sundays'; true })
If you need a nice ignore message in the test report, it gets more complicated. You might need to develop your own annotation-based Spock extension which would react to your custom @MyIgnoreIf(value = { true }, reason = 'whatever')
and also somehow hook into report generation, which might be possible, but I never tried.
Besides, the reason that Spock 2.x can offer users skip reasons more easily is that its engine runs on top of the JUnit 5 platform, which has a Node.SkipResult.skip(String reason)
method out of the box that Spock can delegate the skip reason to.
Spock 1.x however runs on top of JUnit 4, where there is no such infrastructure. Instead, you could use JUnit 4 assumptions, which are basically the equivalent of dynamic skip conditions with reasons, but that would be code-based rather than annotation-based, e.g.:
def dummy() {
org.junit.Assume.assumeFalse('back-end server unavailable on Sundays', true)
expect:
true
}
The output would be something like this and the rest of your test method execution skipped:
org.junit.AssumptionViolatedException: back-end server unavailable on Sundays
at org.junit.Assume.assumeTrue(Assume.java:68)
at org.junit.Assume.assumeFalse(Assume.java:75)
at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:234)
at de.scrum_master.stackoverflow.q74575745.AuthRequestInterceptorTest.dummy(AuthRequestInterceptorTest.groovy:26)
In IntelliJ IDEA, it looks like this, the method actually being reported as skipped:
I think that JUnit 4 assumptions are good enough for your legacy code base, until you upgrade them to Spock 2.x and can enjoy all of the nice new syntax features and extensions.
Upvotes: 2