2012-07-31 74 views
2

我有一个Specflow方案类似下面我可以在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?

+0

你的意思是重复使用步骤,例如“鉴于我有一枪有两颗子弹”? – 2012-08-02 00:23:10

+0

否重用整个方案有点像功能。 – pengibot 2012-08-02 08:40:01

回答

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   | 

(该/人的在‘/邮编/’和‘/数字/’应是“<”和‘>’符号):这可以通过使用一个场景概要和实施例来进行

+0

想象一下,你有一个预订确认系统,有20个步骤。我想测试步骤20做了一些特别的事情,如果我在步骤1中输入了无效值,但我并不在乎步骤2到步骤19之间的内容。我希望能够说出 我为步骤1输入了狡猾的数据。 <我称之为执行步骤2-19>的场景。 我在步骤20检查数据。 这是一个组成的例子,但我想做的一种。我已经写了一个步骤2-19的场景,为什么我要重写所有18个步骤来完成我的场景呢? – pengibot 2012-08-09 08:37:54

+0

我知道我可以通过几种不同的方式来完成这个任务,比如在我编写一个包含所有20个步骤的场景的示例中,然后插入不同的示例数据。我知道这一点,但只是想知道是否可以调用场景,因为文档相当稀疏,具有specflow – pengibot 2012-08-09 08:42:52

+0

我会做的是创建1步,执行步骤2-19从不同的方案。就像'处理数据'一样,您可以在其中调用执行所有这些步骤所需的所有方法。所以你只需要调用'和处理数据',你想要执行第2步到第19步...... 据我所知,你需要的解决方案(类似'和场景x被执行',从.feature文件直接读取完整场景)是不可能的... – TimothyHeyden 2012-08-09 10:58:29

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

相关问题