【发布时间】:2018-05-10 12:37:29
【问题描述】:
有谁知道 cucumber/Java 是否支持多步骤访问同一个 stepDefinition 的功能?
我想要实现的是使用一个 Given 一个步骤,该步骤目前被用作一个 When(在另一个更复杂的测试中,当前的 When 只是一个先决条件)
根据我以前的经验 (Specflow/.Net),我可以使用类似的东西
[Given(@"the user added a (.*) to the cart")]
[When(@"the user adds a (.*) to the cart")]
public void WhenTheUserAddsATotheCart(string product){
////code that adds to the cart
}
如果我使用与 Specflow 中相同的方法,则会引发 DuplicateStepDefinitionException。 因为在将步骤与步骤定义匹配时,Cucumber 没有考虑关键字(Given、When 等)。 Cucumber 只在步骤定义中寻找一个正则表达式,所以我只需要一个步骤定义(给定或何时)。意义
[When(@"the user adds a (.*) to the cart")]
也会匹配
[Given(@"the user adds a (.*) to the cart")]
我的问题是我们使用过去时的 Given 和现在时的 When。
所以问题是:如何对两个步骤定义使用相同的实现?
我看到的唯一解决方案是将实现 Given 的方法的包含提取到另一个方法中,并在 Given 步骤定义和 When.Something 下调用它:
[Given(@"the user added a (.*) to the cart")]
public void GivenTheUserAddedATotheCart(string product){
addToTheCart(product);
}
[When(@"the user adds a (.*) to the cart")]
public void WhenTheUserAddsATotheCart(string product){
addToTheCart(product);
}
public void addToTheCart(String product){
////code that adds to the cart
}
...但我真的不觉得这是一个很好的解决方案
【问题讨论】:
-
我知道这不是在回答您的问题,但以防万一,如果您想切换到(IMO 高级)组合 Python-Behave,那么您可以使用通用 STEP 关键字代替 GIVEN , WHEN 或 THEN。
标签: cucumber