两个步骤定义相同,但一个用于给定,一个用于然后 [英] Two Step Definitions that are the same but one is for Given and one is for Then

查看:80
本文介绍了两个步骤定义相同,但一个用于给定,一个用于然后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试具有两个相似的步骤定义但具有不同的注释,则黄瓜特征文件将引发错误(声明了多个步骤定义)-

Cucumber feature files are throwing an error (multiple step definitions declared) if I try to have two similar step definitions but with different annotations -

@Then("^element with id \"([^\"]*)\"  is displayed$")

&

@Given("^element with id \"([^\"]*)\" is displayed$")

到目前为止,我已经通过在@Then定义中放置一个额外的空格(在正则表达式之后仔细查看@Then)来欺骗它。

So far I've fooled it by putting an extra space in the @Then definition (look carefully at @Then, after the regex).

但这不是一个好习惯。

我想要@Given,因为它设置了我的方案,我肯定需要@Then。

I want the @Given, because it sets up up my scenario, and I definitely need the @Then.

如何通过此操作?

推荐答案

步骤定义对于Cucumber知道执行什么内容必须是唯一的。此外,Given / When / Then关键字在技术上可以互换。它们只是为了功能文件的可读性,而不是与实现相关联。因此,如果两个步骤(给定和然后)都执行相同的操作,则从技术上讲没有问题;您应该可以在功能文件中使用相同的步骤定义,并以 Given或 Then关键字开头。
也就是说,您可能要考虑重写步骤定义以描述预期的行为,而不是实现,例如给出ID为xxx的元素

Step Definitions have to be unique for Cucumber to know what to execute. In addition, the Given/When/Then keywords are technically interchangeable. They're for the readability of the feature file, but not linked to the implementation. So if both steps (Given and Then) do the same thing, there is technically no problem; you should be able to use the same step definition from your feature file, preceded by either 'Given' or 'Then' keyword. That said, you might want to consider rewriting your step definitions to describe the intended behavior instead of the implementation, e.g. "Given an element with id xxx"

这篇关于两个步骤定义相同,但一个用于给定,一个用于然后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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