在我的测试中,我有一些只需要在某些情况下运行的功能方法。我的代码看起来像这样:
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()
}
}
Run Code Online (Sandbox Code Playgroud)
我应该注意,我使用的是自定义 spock 扩展,即使以前的功能方法失败,它也允许我运行规范的所有功能方法。
我刚刚意识到的事情以及我发表这篇文章的原因是因为“功能方法 2”由于某种原因没有出现在我的测试结果中,但方法 1 和 3 却出现了。即使someCondition设置为true,它也不会出现在构建结果中。所以我想知道为什么会这样,以及如何使这个功能方法成为有条件的
Spock 对条件执行功能有特殊支持,请查看@IgnoreIf和@Requires。
@IgnoreIf({ os.windows })
def "I'll run everywhere but on Windows"() { ... }
Run Code Online (Sandbox Code Playgroud)
您还可以在条件闭包中使用静态方法,它们需要使用限定版本。
class MyTest extends GebReportingSpec {
@Requires({ MyTest.myCondition() })
def "I'll only run if myCondition() returns true"() { ... }
static boolean myCondition() { true }
}
Run Code Online (Sandbox Code Playgroud)