如果我们有多个 class 扩展规范,则断言 false 不会停止测试的执行
Assert false not stopping the execution of Tests if we have multiple class extending Specification
假设我们有 Parent 和 Child class,我们在 Parent 中扩展了规范并且 Child 扩展了 Parent。因此 Child 也成为 Specification.
//Parent
@Stepwise
class Parent extends Specification{
@Shared
String name
def setupSpec(){
println "inside setupSpec"
}
def setup(){
println "inside SetUp"
}
def "testMethodOne"(){
given:
println "inside parent testmethodOne"
assert 1==2
}
def "testMethodTwo"(){
given:
println "parent testmethodTwo"
}
def cleanup(){
println " inside CleanUp"
}
def cleanupSpec(){
println "inside cleanSpec"
}
}
//ChildClass
//Child
@Stepwise
class Child extends Parent {
def "testMethod"(){
given:
println "inside child testmethod"
}
def "testMethodtwo"(){
given:
println "inside child testmethodTeo"
}
}
现在,如果我们正在执行 Child Class 那么断言将在 Parents testMethodOne 中失败,并且由于我们正在使用 @Stepwise 那么整个测试不应该在失败后执行断言。有趣的是 parent 的测试方法没有被执行,因为所有的方法都被执行 child 而不是因为断言失败而执行。
如果我遗漏了什么,请告诉我。
package spock.lang;
import java.lang.annotation.*;
import org.spockframework.runtime.extension.ExtensionAnnotation;
import org.spockframework.runtime.extension.builtin.StepwiseExtension;
/**
* 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.
*
* <p><tt>@Stepwise</tt> 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.
*
* @author Peter Niederwieser
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtensionAnnotation(StepwiseExtension.class)
public @interface Stepwise {}
在超和子规范中声明的特征方法不受影响。
测试方法在 Spock 中称为特征方法。所以一切都很好,因为它是这样设计的。
如果您想在父测试失败时让您的测试套装失败,请向子测试添加自定义侦听器:
@Stepwise
class Child extends Parent {
def setupSpec() {
def spec = this.getSpecificationContext().currentSpec
spec.addListener(new AbstractRunListener() {
@Override
void error(ErrorInfo error) {
spec.features.each { it.skipped = true }
}
})
}
def "test"() {
given:
println "Child::test#1"
}
def "test#2"() {
given:
println "Child::test#2"
}
}
假设我们有 Parent 和 Child class,我们在 Parent 中扩展了规范并且 Child 扩展了 Parent。因此 Child 也成为 Specification.
//Parent
@Stepwise
class Parent extends Specification{
@Shared
String name
def setupSpec(){
println "inside setupSpec"
}
def setup(){
println "inside SetUp"
}
def "testMethodOne"(){
given:
println "inside parent testmethodOne"
assert 1==2
}
def "testMethodTwo"(){
given:
println "parent testmethodTwo"
}
def cleanup(){
println " inside CleanUp"
}
def cleanupSpec(){
println "inside cleanSpec"
}
} //ChildClass
//Child
@Stepwise
class Child extends Parent {
def "testMethod"(){
given:
println "inside child testmethod"
}
def "testMethodtwo"(){
given:
println "inside child testmethodTeo"
}
}
现在,如果我们正在执行 Child Class 那么断言将在 Parents testMethodOne 中失败,并且由于我们正在使用 @Stepwise 那么整个测试不应该在失败后执行断言。有趣的是 parent 的测试方法没有被执行,因为所有的方法都被执行 child 而不是因为断言失败而执行。
如果我遗漏了什么,请告诉我。
package spock.lang;
import java.lang.annotation.*;
import org.spockframework.runtime.extension.ExtensionAnnotation;
import org.spockframework.runtime.extension.builtin.StepwiseExtension;
/**
* 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.
*
* <p><tt>@Stepwise</tt> 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.
*
* @author Peter Niederwieser
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtensionAnnotation(StepwiseExtension.class)
public @interface Stepwise {}
在超和子规范中声明的特征方法不受影响。
测试方法在 Spock 中称为特征方法。所以一切都很好,因为它是这样设计的。
如果您想在父测试失败时让您的测试套装失败,请向子测试添加自定义侦听器:
@Stepwise
class Child extends Parent {
def setupSpec() {
def spec = this.getSpecificationContext().currentSpec
spec.addListener(new AbstractRunListener() {
@Override
void error(ErrorInfo error) {
spec.features.each { it.skipped = true }
}
})
}
def "test"() {
given:
println "Child::test#1"
}
def "test#2"() {
given:
println "Child::test#2"
}
}