Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make a feature method conditional

Tags:

spock

geb

In my Test, I have some feature methods that only need to run in certain situations. My code looks something like this:

class MyTest extends GebReportingSpec{

    def "Feature method 1"(){
        when:
        blah()
        then:
        doSomeStuff()
    }

    def "Feature method 2"(){
        if(someCondition){
            when:
            blah()
            then:
            doSomeMoreStuff()
        }
    }

    def "Feature method 3"(){
        when:
        blah()
        then:
        doTheFinalStuff()
    }
}

I should note I am using a custom spock extension that allows me to run all feature methods of a spec even if a previous feature method fails.

The thing I just realized and the reason I am making this post, is because "Feature method 2" does not show up in my test results for some reason, but method 1 and 3 do. Even if someCondition is set to true, it does not appear in the build results. so I am wondering why this is, and how I can make this feature method conditional

like image 466
switch201 Avatar asked Dec 21 '25 03:12

switch201


1 Answers

Spock has special support for conditionally executing features, take a look at @IgnoreIf and @Requires.

@IgnoreIf({ os.windows })
def "I'll run everywhere but on Windows"() { ... }

You can also use static methods in the condition closure, they need to use the qualified version.

class MyTest extends GebReportingSpec {
  @Requires({ MyTest.myCondition() })
  def "I'll only run if myCondition() returns true"() { ... }

  static boolean myCondition() { true }
}
like image 50
Leonard Brünings Avatar answered Dec 24 '25 09:12

Leonard Brünings