2016-12-16 7 views
0

상위 및 하위 클래스가 확장되었지만 부모 및 하위 클래스가 확장 부모를 가졌다 고 가정합니다. 따라서 어린이는 또한 becames 사양입니다.Assert 우리가 여러 클래스를 확장하는 경우 테스트 실행을 중지하지 않음 사양

//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" 

} 

} // 자식 클래스 이제

//Child 
@Stepwise 
class Child extends Parent { 

def "testMethod"(){ 
    given: 
    println "inside child testmethod" 

} 
def "testMethodtwo"(){ 
    given: 
    println "inside child testmethodTeo" 

} 
} 

우리는 우리가 다음 전체 시험 후에 실행하지 말았어야 @Stepwise을 사용하고 있기 때문에 자식 클래스 다음 부모 testMethodOne에 실패 주장하고 실행하는 경우 단언의 실패. 흥미롭게도 부모 메소드가 실행되지 않습니다. 모든 메소드가 하위로 실행되는 경우 실행되지 않는 assert가 실패했습니다.

제가 누락 된 항목이 있으면 알려주십시오.

+0

은 당신이 주장 처리하면 컴파일 할 때 켜져 있습니까? 일부 IDE/컴파일러에는 기본적으로이 기능이 있습니다. – rossum

답변

1
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>&#64;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 {} 

super 및 subspecs에 선언 된 기능 메서드는 영향을받지 않습니다.

시험 방법은 Spock에서 피처 방법이라고합니다. 모든 것이 잘되었으므로 설계 상으로는이 방식으로 작동합니다. 부모 테스트가 실패

당신이 당신의 테스트 슈트에 실패 할 경우, 아이 테스트에 사용자 정의 listerner을 추가

@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" 
    } 

} 
+0

아동 검사를 건너 뛸 수있는 방법이 있습니까? –

+0

사용자 지정 수신기를 추가하십시오. 나는 그 대답에 대한 경청자의 예를 덧붙였다. –

+0

currentSpec이 솔루션에서 해결되지 않습니다. –