本文介绍了重用黄瓜步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重用一些 Cucumber 步骤,但似乎找不到正确的方法.

I want to reuse some Cucumber steps but can't seem to find the right way.

我想写一个这样的步骤:

I want to write a step like:

Given /^I login with (.*) credentials$/ |type|
  # do stuff with type being one of "invalid" or "valid"
end

然后还有另一个步骤:

Given /^I login successfully$
  # call "Given I login with valid credentials"
end

所以在测试用户身份验证时,我可以使用前者,但在大多数其他地方,我可以使用后者,而实际上不必重新编码.

So in testing user authentication I can use the former, but most other places, I can use the latter, and not actually have to repro code.

有没有办法调用其他步骤,或者我只是将逻辑放在辅助方法中,然后从每个任务中调用所述方法(基本上是方法提取重构,阅读我的问题后让我相信这实际上是最好的方法)?

Is there a way to call that other step, or do I just put the logic in a helper method, and call said method from each task (basically a method extraction refactoring, which, after reading my question makes me believe that's actually the best way anyway)?

推荐答案

UPDATE:下面描述的方法已被弃用.从另一个步骤中调用一个步骤的推荐方法现在看起来像这样:

UPDATE: The method described below has been deprecated. The recommended way to call a step from within another step now looks like this:

Given /^I login successfully$/
    step "I login with valid credentials"
end

不推荐使用的旧方法(供参考):

您可以像这样从其他步骤调用步骤:

You can call steps from other steps like this:

Given /^I login successfully$/
  Given "I login with valid credentials"
  Then "I should be logged in"
end

如果功能中的所有场景都需要此(或其他步骤),您还可以为每个功能添加背景,使用通用步骤,如下所示:

If all of the scenarios within a feature require this (or other steps), you can also add a Background to each features, with the common steps, like so:

Background:
  Given I log in with valid credentials

Scenario: Change my password
  Given I am on the account page

这篇关于重用黄瓜步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:29