2016-12-16 5 views
0

親クラスと子クラスが拡張されているとします。従って子供はまたbecames仕様です。複数のクラスを拡張している場合、テストの実行を停止しないfalseを指定します。指定

//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に失敗主張して実行している場合アサーションの失敗。興味深いことに、親のメソッドが実行されていないということは、すべてのメソッドが子として実行されていて、アサートが失敗した場合に実行されないメソッドです。

何か不足している場合はお知らせください。

+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 {} 

スーパーおよびサブスペックで宣言されたフィーチャメソッドは影響を受けません。

試験方法は、スポックにおける特徴メソッドと呼ばれています。このように設計されているので、すべてが問題ありません。

あなたは親のテストが失敗した場合、テストスーツを失敗し、子テストにカスタム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このソリューションでは解決されません。 –

関連する問題