我可以在不重复使用Cucumber的步骤的情况下重复这些步骤吗?

问题描述:

我需要测试用户多次重复执行一组操作时触发的行为。我想用一个看起来像这样的情况下,结束了:我可以在不重复使用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应用程序不应该崩溃。这个例子是弥补的。 –

如果你有超过步骤定义代码控制尝试这样的一步 -

And I press Continue button and go back 5 times 

捕获的具有可变步骤定义的次数和调用现有功能在一个循环的次数。

+0

这是一种方式,但这意味着我必须为每个需要重复某些步骤的场景创建一个新的步骤定义。我想这是可以接受的,但并不理想。 –

+1

那么你甚至可以在变量中看到“继续”部分和“后退”部分,并查看是否可以在具有不同操作对的类似场景中重复使用。或者发挥创造力并设置一个行动工厂,您可以使用您在场景中使用的任何关键字实例化工厂。 – Grasshopper

要重复任意步骤,而不必每次都写一个新的,定义这一步

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的建议去了,因为它使得这个场景更具可读性。 –