重复使用黄瓜步骤 [英] Reuse Cucumber steps

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

问题描述

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

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

我想写一个步骤,如:

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 






旧的,已弃用的方法(供参考):


Old, deprecated method (for reference):

您可以从其他步骤调用以下步骤:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆