4

次のようなSpecflowシナリオがあります

Scenario: I Shoot a gun
When I pull the trigger
Then It should expel a bullet from the chamber

私が望んでいるのは、以下のコードのようにこのシナリオを再利用することです

Scenario: I Shoot a gun till there are no bullets left
    Given I have a fun with 2 bullets in
    And I Shoot a gun
    And I Shoot a gun
    Then There should be no bullets left in the gun

その瞬間、私はシナリオのすべてのステップを繰り返さなければなりません。私は次のように銃を撃ちます

Scenario: I Shoot a gun till there are no bullets left
     Given I have a fun with 2 bullets in
 When I pull the trigger
 Then It should expel a bullet from the chamber
 When I pull the trigger
 Then It should expel a bullet from the chamber
     Then There should be no bullets left in the gun

上記のシナリオでは、2ステップしか節約できませんが、実際のアプリケーションでは、場合によっては20ステップ以上の書き換えを節約できます。シナリオを呼び出すことができると、読みやすくなり、隠れた手順について心配する必要がなくなると思います。

これはSpecflowで可能ですか?

4

2 に答える 2

3

まったく同じテストを何度も再利用したい理由が思いつかないので、異なるパラメーターでシナリオを再利用したいと思います。これは、シナリオの概要と例を使用して実行できます。

シナリオの概要:人が住所を提供しています
    私がアドレスページにいるとすると
    そして、私は/zipcode/をzipcodeフィールドに入力しました
    そして、house_numberフィールドに/number/を入力しました
    nextStepを押すと
    次に、確認ページにリダイレクトする必要があります
例:
    | 郵便番号| 番号|
    | 666 | 1 |
    | 666 | 2 |
    | 666 | 3 |
    | 555 | 2 |

(「/zipcode/」および「/number/」の/は、「<」および「>」記号である必要があります)

于 2012-08-07T13:30:19.810 に答える
1

私が理解していることから、あなたは次のように言う能力が必要です。

Scenario: I Shoot a gun till there are no bullets left
    Given I have a fun with 2 bullets in
     When I shoot the gun 2 times
     Then There should be no bullets left in the gun

別のステップ内からステップを呼び出すことができます。これは、 「銃を2回撃ったとき」のステップ定義で確認できます。

[When(@"I shoot the gun (*.) times")]
public void WhenIShootTheGunNTimes(int times)
{
    // Fire the gun the specified number of times.
    for ( int i = 0; i < times; i++ )
    {
        // Call the other two steps directly in code...
        WhenIPullTheTrigger();
        ThenItShouldExpelABulletFromTheChamber();
    }
}

ガーキンで指定した回数だけ他のステップを呼び出します。C#でメソッドを直接呼び出すことにしました。または、ここで指定されているように、ガーキンを使用して間接的に呼び出すこともできます。

于 2012-10-10T03:42:29.097 に答える