2016-08-19 70 views
2

我需要測試用戶多次重複執行一組操作時觸發的行爲。我想用一個看起來像這樣的情況下,結束了:我可以在不重複使用Cucumber的步驟的情況下重複這些步驟嗎?

Scenario: Nice way 
    Given that I am on some screen 
    When I enter something into some text field 
    And I press the Continue button 
    And I go back 
    And I repeat the previous 2 steps 5 times 
    Then the application should crash 

,而不是一個場景,看起來像這樣:

Scenario: Annoying way 
    Given that I am on some screen 
    When I enter something into some text field 
    And I press the Continue button 
    And I go back 
    And I press the Continue button 
    And I go back 
    And I press the Continue button 
    And I go back 
    And I press the Continue button 
    And I go back 
    And I press the Continue button 
    And I go back 
    And I press the Continue button 
    And I go back 
    Then the application should crash 

是否有這樣做的標準方法或做我有自己實現它?

+0

這就是你在這裏指定的一個很好的應用程序('然後應用程序應該崩潰了) – SirLenz0rlot

+0

爲什麼你想讓應用程序崩潰?這似乎是一個探索性測試,而不是設計測試。我不會在黃瓜裏做。 –

+0

@DaveMcNulla應用程序不應該崩潰。這個例子是彌補的。 –

回答

3

如果你有超過步驟定義代碼控制嘗試這樣的一步 -

And I press Continue button and go back 5 times 

捕獲的具有可變步驟定義的次數和調用現有功能在一個循環的次數。

+0

這是一種方式,但這意味着我必須爲每個需要重複某些步驟的場景創建一個新的步驟定義。我想這是可以接受的,但並不理想。 –

+1

那麼你甚至可以在變量中看到「繼續」部分和「後退」部分,並查看是否可以在具有不同操作對的類似場景中重複使用。或者發揮創造力並設置一個行動工廠,您可以使用您在場景中使用的任何關鍵字實例化工廠。 – Grasshopper

1

要重複任意步驟,而不必每次都寫一個新的,定義這一步

When /^I repeat "([^"]*)" (\d+) times$/ do |steps, times| 
    times.to_i.times do 
    steps.split(/;\s*/).each do |step| 
     step step 
    end 
    end 
end 

,並使用它像這樣:

When I repeat "press the Continue button; go back" 5 times 

(不要使用分號)

+0

謝謝,但我用Grasshopper的建議去了,因爲它使得這個場景更具可讀性。 –

相關問題