我可以在Specflow中重用方案吗?

时间:2012-07-31 15:40:56

标签: specflow

我有一个Specflow Scenario,如下所示

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中是否可行?

2 个答案:

答案 0 :(得分:3)

因为我想不出你想要再次使用相同测试的原因,我假设你想要重用具有不同参数的场景。这可以通过使用场景大纲和示例来完成:

Scenario Outline: Person is supplying an address
    Given I am on the address page
    And I have entered /zipcode/ into the zipcode field
    And I have entered /number/ into the house_number field
    When I press nextStep
    Then I should be redirected to the confirmation page
Examples:
    | zipcode  | number     |
    | 666      | 1          |
    | 666      | 2          |
    | 666      | 3          |
    | 555      | 2          |

(“/ zipcode /”和“/ number /”中的/应该是'<'和'>'符号)

答案 1 :(得分: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#中调用方法。您也可以使用指定here的小黄瓜间接调用它们。