I think a good enhancement would be the ability to skip the setup() for particular tests. For example I have 20 tests but 1 or 2 of them are slightly different and don't need the setup(). Perhaps a flag ofÂ--Âsetup: falseon the method or something like that. Just throwing the idea out there as I have had several time this has come up for me. We can easily get around it by just creating a method of what we want and putting it in all the tests except the ones that don't need it but I thought maybe others have come across this. Just want to throw an enhancement idea out there.
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/306afc9f-0883-4a40-b16c-e786dbfde9a4%40googlegroups.com.
Hi Jeremy.
I agree with Marcin in everything he says. You really should refactor the way he suggested. And as he also said the first time he answered your question, this is a Spock rather than a Geb question.
But for what it is worth, I was a little bit bored just now and wanted to find out if an annotation-driven extension could solve the problem. Actually to quickly hack
is a matter of two minutes.
The complication here is the fact that actually nothing specific should happen in the annotated method itself but in the feature's setup() method which gets executed at another time (later, more precisely). So we have to find a way to communicate to the setup() method that we want to skip it. The way I did it is a bit contrived, but easy enough to implement:
Here is the source code for
package de.scrum_master.testing
import org.spockframework.runtime.extension.ExtensionAnnotation
import java.lang.annotation.Retention
import java.lang.annotation.Target
import static java.lang.annotation.ElementType.METHOD
import static java.lang.annotation.RetentionPolicy.RUNTIME
@Retention(RUNTIME)
@Target(METHOD)
@ExtensionAnnotation(SkipSetupExtension)
@interface SkipSetup {}
package de.scrum_master.testing
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
import org.spockframework.runtime.model.FeatureInfo
class SkipSetupExtension extends AbstractAnnotationDrivenExtension<SkipSetup> {
SkipSetupMethodInterceptor interceptor
@Override
void visitFeatureAnnotation(SkipSetup annotation, FeatureInfo feature) {
if (!interceptor) {
interceptor = new SkipSetupMethodInterceptor()
feature.spec.addSetupInterceptor interceptor
}
interceptor.skippedFeatures << feature.name
}
}
package de.scrum_master.testing
import org.spockframework.runtime.extension.AbstractMethodInterceptor
import org.spockframework.runtime.extension.IMethodInvocation
class SkipSetupMethodInterceptor extends AbstractMethodInterceptor {
List<String> skippedFeatures = new LinkedList<>()
@Override
void interceptSetupMethod(IMethodInvocation invocation) throws Throwable {
if (!skippedFeatures.contains(invocation.feature.name))
invocation.proceed()
}
}
package de.scrum_master.testing
import spock.lang.Specification
class SkipSetupTestA extends Specification {
def setup() {
println "SkipSetupTestA -> setup"
}
def feature1() {
println "SkipSetupTestA -> feature1"
expect: true
}
@SkipSetup
def feature2() {
println "SkipSetupTestA -> feature2"
expect: true
}
def feature3() {
println "SkipSetupTestA -> feature3"
expect: true
}
@SkipSetup
def feature4() {
println "SkipSetupTestA -> feature4"
expect: true
}
}
package de.scrum_master.testing
import spock.lang.Specification
class SkipSetupTestB extends Specification {
def setup() {
println "SkipSetupTestB -> setup"
}
def feature1() {
println "SkipSetupTestB -> feature1"
expect: true
}
@SkipSetup
def feature2() {
println "SkipSetupTestB -> feature2"
expect: true
}
def feature3() {
println "SkipSetupTestB -> feature3"
expect: true
}
@SkipSetup
def feature4() {
println "SkipSetupTestB -> feature4"
expect: true
}
}
I am also attaching a ZIP file with the source code for your convenience. 😃
Let us complete the picture with the console log output when running both specifications together:
SkipSetupTestA -> setup
SkipSetupTestA -> feature1
SkipSetupTestA -> feature2
SkipSetupTestA -> setup
SkipSetupTestA -> feature3
SkipSetupTestA -> feature4
SkipSetupTestB -> setup
SkipSetupTestB -> feature1
SkipSetupTestB -> feature2
SkipSetupTestB -> setup
SkipSetupTestB -> feature3
SkipSetupTestB -> feature4
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/CA%2B52dQQt_i-1EESWL9fjO3ZMB5FwFXCaeFKb46nn4eg_R2oG%2Bw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/20200503162255.48EAC44C06BC%40dd39516.kasserver.com.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/306afc9f-0883-4a40-b16c-e786dbfde9a4%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/20200511024825.82D3144C099E%40dd39516.kasserver.com.
Definitely simpler, yes. Thanks for this wonderfully elegant solution. :-)
For the benefit of everyone reading this who forgot to check the Spock manual's chapter about global extensions: You will also need a file META-INF/services/org.spockframework.runtime.extension.IGlobalExtension with this content:
de.scrum_master.testing.extension.SkipSetupGlobalExtension
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/CA%2B52dQTt_8NUefxr9P_iZTMm9rWv8v78kFgo2jPeQfUhUN9X_g%40mail.gmail.com.
>>>> an email to geb-...@googlegroups.com
>>>> <mailto:geb-...@googlegroups.com> .
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/geb-user/306afc9f-0883-4a40-b16c-e786dbfde9a4%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/geb-user/306afc9f-0883-4a40-b16c-e786dbfde9a4%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Geb User Mailing List" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to geb-...@googlegroups.com
>>> <mailto:geb-...@googlegroups.com> .
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/geb-user/CA%2B52dQQt_i-1EESWL9fjO3ZMB5FwFXCaeFKb46nn4eg_R2oG%2Bw%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/geb-user/CA%2B52dQQt_i-1EESWL9fjO3ZMB5FwFXCaeFKb46nn4eg_R2oG%2Bw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Geb User Mailing List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to geb-...@googlegroups.com
>> <mailto:geb-...@googlegroups.com> .
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/geb-user/20200503162255.48EAC44C06BC%40dd39516.kasserver.com
>> <https://groups.google.com/d/msgid/geb-user/20200503162255.48EAC44C06BC%40dd39516.kasserver.com?utm_medium=email&utm_source=footer>
>> .
>
> --
> You received this message because you are subscribed to the Google Groups
> "Geb User Mailing List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to geb-...@googlegroups.com
> <mailto:geb-...@googlegroups.com> .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/geb-user/CA%2B52dQRzDpeqWzOpN-k2o%3DE_dhBGUAQRDZKw%2By3hkB_oHK1eCA%40mail.gmail.com
> <https://groups.google.com/d/msgid/geb-user/CA%2B52dQRzDpeqWzOpN-k2o%3DE_dhBGUAQRDZKw%2By3hkB_oHK1eCA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/56088b0c-0999-45ab-9529-63609e593592%40googlegroups.com.