我是 Cucumber(jvm) 的新手,一切看起来都很好,但是 :
我真的不知道如何通过单一方法实现以各种方式(优雅地)编写的多个初始条件(来自各种场景)。
例如:
Scenario: I really am bad
Given I really am inexperienced with Cucumber
When I try to work
Then what I produce is of poor quality
Scenario: I am on the way to become good (hopefully)
Given I am a noob
When I learn new things
And I practice
Then my level improves
因为 鉴于我对 Cucumber 真的没有经验
和 鉴于我是一个 cuke noob
(虽然在语义上相同)对我来说足够接近要以完全相同的方式实现,我希望能够将它们链接到相同的方法但是
@Given("^I really am inexperienced with Cucumber$")
@Given("^I am a cuke noob$")
public void checkMyLevelIsGenerallyLow() throws Throwable {
// some very clever code to assess then confirm my mediocre level ... something like if(true) ...
}
但上面显示的代码不会编译,因为 cucumber.api.java.en.@Given
注释不是 java.lang.annotation.@Repeatable
...
一个简单的解决方案是做类似的事情
public void checkMyLevelIsGenerallyLow() throws Throwable {
// some very clever code to assess then confirm my mediocre level ... something like if(true) ...
}
@Given("^I really am inexperienced with Cucumber$")
public void check_I_really_am_inexperienced_with_Cucumber() throws Throwable {
checkMyLevelIsGenerallyLow();
}
@Given("^I am a cuke noob$")
public void check_I_am_a_cuke_noob() throws Throwable {
checkMyLevelIsGenerallyLow();
}
这会工作得很好,但需要大量代码来处理简单的事情,我很确定还有其他方法。
甚至,当我问自己写下这个问题时,“我是否只是从正确的角度来解决这个问题?”,这就是我试图在 BDD 方面实现的一个好主意吗?
我认为这并不全是坏事,因为小 cucumber 应该保持语义和句子结构,词汇选择取决于上下文(因此场景)。然而,我应该可以自由地以任何我喜欢的方式实现它。
总结一下:
@Given
应该是@Repeatable
吗?- 如果是,为什么不是?还有别的办法吗?
- 如果不是,我在方法方面缺少什么?
最佳答案
关于多重表达的@given
这可能不是最好的方法,但我挠头想到了:
@Given("^I really am inexperienced with Cucumber$|^I am a cuke noob$")
public void checkMyLevelIsGenerallyLow() throws Throwable {
// some very clever code to assess then confirm my mediocre level ... something like if(true) ...
}
而且有效! 这正是我一直在寻找的,甚至可以像这样变得更具可读性:
@Given("^I really am inexperienced with Cucumber$"+
"|^I am a cuke noob$")
关于不可重复性@given
正如 blalasaadri 所述,@Given
可能是 @Repeatable
,但仅来自 Java8,并且自从 @Repeatable
在 Java8 中引入以来。
特别感谢
Ceiling Gecko 让我记住,最简单和最明显的解决方案通常是最好和最优雅的。
关于java - 为什么@Given 不可重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29539665/