如果 "setup test" 失败,Spock 将忽略测试

Spock ignore test if a "setup test" fail

我正在编写一种集成测试。我需要测试测试的环境是否真的是 运行.

伪代码:

def "setup test"() {
    expect:
    service1.isRunning()
    service2.isRunning()
}

def "test1"() {
    expect:
    service1.something() == 1
    service2.something() == 2
}

def "test2"() {
    // ...
}

基本上,如果 "setup test" 失败了,我什至不想看到 "test1" 和 "test2" 的结果(他们肯定也会失败!)。我怎样才能做到这一点?我试过 @IgnoreIf,但它似乎只是针对我们可以从测试之外提供的条件的解决方案。

您可以尝试使用@Stepwise注解(定义在class级别)来保证顺序,如果失败则跳过剩余的测试。当然,副作用是如果test1失败,test2不会被触发,但这是引入顺序和条件执行的最简单方法。

Indicates that a spec's feature methods should be run sequentially in their declared order (even in the presence of a parallel spec runner), always starting from the first method. If a method fails, the remaining methods will be skipped. Feature methods declared in super- and subspecs are not affected.

@Stepwise is useful for specs with (logical) dependencies between methods. In particular, it helps to avoid consecutive errors after a method has failed, which makes it easier to understand what really went wrong.


Source: http://spockframework.org/spock/javadoc/1.0/spock/lang/Stepwise.html