2012-07-31 78 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

相關問題