我可以重复步骤而不重复使用黄瓜的步骤吗?

时间:2016-08-19 12:03:25

标签: ruby 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

有没有标准的方法可以做到这一点,还是我必须自己实施?

2 个答案:

答案 0 :(得分:4)

如果您可以控制步骤定义代码,请尝试这样的步骤 -

And I press Continue button and go back 5 times

使用变量捕获步骤定义中的次数,并在循环中调用现有函数多次。

答案 1 :(得分: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

(并且不要在要重复的步骤名称中使用分号。)