2017-01-25 11 views
0

明示的な入力パラメータとしてデータテーブルを使用する必要がなく、Spockで現在の実行カウントを透過的に取得する方法はありますか?例Spock実行データテーブル数

class MathSpec extends Specification { 
    def "maximum of two numbers"(int a, int b, int c) { 
     expect: 
     Math.max(a, b) == c 
     println "this is row:" + $currentRowCount//?? 


     where: 
     a | b | c 
     1 | 3 | 3 
     7 | 4 | 4 
     0 | 0 | 0 
    } 
} 

答えて

3

iterationCountはプライベート変数ですが、これは可能です:

例仕様:

package de.scrum_master.app 

import spock.lang.Specification 
import spock.lang.Unroll 

class PieceTest extends Specification { 
    @Unroll 
    def "do something with data table item #item"() { 
    expect: 
    println specificationContext 
     .currentIteration 
     .parent 
     .iterationNameProvider 
     .iterationCount 

    where: 
    item | anotherItem 
    "foo" | 333 
    "bar" | 444 
    "zot" | 555 
    } 
} 

コンソールログ:

1 
2 
3 

これだけではないという注釈なしに、@Unroll編の機能方法のために働くことに注意してください。


更新:グローバルスポックの延長を経て

免責事項を繰り返し回数を公開:私はスポックを愛するが、私はGroovyのメタプログラミングに堪能ではないです。これは私の最初のSpock拡張です。

あなたは、主に、アノテーション・ドリブンですbuilt-in Spock extensions、など@Ignore@Timeout@Stepwise@Issue@AutoCleanupを知っているかもしれません。しかし、マニュアルには記載されていないが、依然として存在するグローバルエクステンションも存在する。 ReportLogExtension,IncludeExcludeExtension

このマニュアルでは、このような拡張機能の作成方法については説明していませんが、見つけ出すことのできるグーグルとソースコードのリサーチがあります。しかし初心者のためのものではありません。

グローバルスポック拡張:

この拡張は、各スポック仕様に動的部材iterationCountを加算します。

package de.scrum_master.app 

import org.spockframework.runtime.AbstractRunListener 
import org.spockframework.runtime.extension.AbstractGlobalExtension 
import org.spockframework.runtime.model.FeatureInfo 
import org.spockframework.runtime.model.IterationInfo 
import org.spockframework.runtime.model.SpecInfo 

class IterationCountExtension extends AbstractGlobalExtension { 
    @Override 
    void visitSpec(SpecInfo spec) { 
    spec.addListener(new IterationCountListener()) 
    } 

    static class IterationCountListener extends AbstractRunListener { 
    MetaClass metaClass 
    int iterationCount 

    @Override 
    void beforeSpec(SpecInfo spec) { 
     println spec.name 
     metaClass = spec.reflection.metaClass 
    } 

    @Override 
    void beforeFeature(FeatureInfo feature) { 
     println " " + feature.name 
     iterationCount = 0 
     metaClass.iterationCount = iterationCount 
    } 

    @Override 
    void beforeIteration(IterationInfo iteration) { 
     println " " + iteration.name 
     metaClass.iterationCount = iterationCount++ 
    } 
    } 
} 

各拡張子を登録する必要があります。だから、また、テストリソースに次の内容のファイルMETA-INF/services/org.spockframework.runtime.extension.IGlobalExtensionを追加してください:

de.scrum_master.app.IterationCountExtension 

ショーケース仕様:

package de.scrum_master.app 

import spock.lang.Specification 
import spock.lang.Unroll 

class SampleTest extends Specification { 
    def "no data table"() { 
    expect: 
    println "  " + iterationCount 
    } 

    def "data table items"() { 
    expect: 
    println "  " + iterationCount 

    where: 
    item | anotherItem 
    "foo" | 333 
    "bar" | 444 
    "zot" | 555 
    } 

    @Unroll 
    def "unrolled data table item"() { 
    expect: 
    println "  " + iterationCount 

    where: 
    item | anotherItem 
    "foo" | 333 
    "bar" | 444 
    "zot" | 555 
    } 

    @Unroll 
    def "unrolled data table item #item"() { 
    expect: 
    println "  " + iterationCount 

    where: 
    item | anotherItem 
    "foo" | 333 
    "bar" | 444 
    "zot" | 555 
    } 
} 

コンソールログ:

SampleTest 
    no data table 
    no data table 
     0 
    data table items 
    data table items 
     0 
    data table items 
     1 
    data table items 
     2 
    unrolled data table item 
    unrolled data table item[0] 
     0 
    unrolled data table item[1] 
     1 
    unrolled data table item[2] 
     2 
    unrolled data table item #item 
    unrolled data table item foo 
     0 
    unrolled data table item bar 
     1 
    unrolled data table item zot 
     2 

することができますように参照してください、@Unrollが使用されているかどうか。この点では、上記よりも速い&の汚れた溶液よりも優れています。

ところで、カウントを0ベースではなく1ベースにする場合は、拡張リスナーメソッドbeforeIterationの式iterationCount++++iterationCountに変更してください。

+0

ニースが見つかりました!私はソースを掘り下げていましたが、そこには行きませんでした;-)他の注意点は、Spockの内部構造に依存しないことですが、努力のために+1してください;-) –

+0

私は、 IntelliJ IDEAではブレークポイントとデバッガを使用していました。ケーキの一片。 ;-) – kriegaex

+0

私は答えにグローバルSpock拡張を利用したソリューションのバリエーションを追加しました。あなたが好きならチェックしてください。これは私のクイック&汚れた解決策よりもはるかに楽しく学び、実装することができました。 :)) – kriegaex

1

については

あなたは、#iterationCount@Unrollを使用することができ、すなわち:

import spock.lang.* 

class MathSpec extends Specification { 

    @Unroll 
    def "maximum of #a and #b is #c (iteration #iterationCount)"() { 
     given: 
     def result = Math.max(a, b) 

     expect: 
     result == c 

     where: 
     a | b | c 
     1 | 3 | 3 
     7 | 4 | 4 
     0 | 0 | 0 
    } 
} 

PS:あなたのクラスMathを呼び出さないでください、それから呼び出そうMath.max ;-)

+0

しかし、iterationCountをspec本体に使用することはできません。 – Opal

+0

@tim_yates - ありがとうございますが、必要なものであるメソッド本体のiterationCountにアクセスできません。 – Ian