作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经(非常残酷地)注意到,对于 cucumber-JVM,当您定义其步骤具有看似相同名称的功能时,它们被认为是可互换的。
Feature: Grab that cash with both hands and make a stash
Scenario: Spend it
Given Jack has 5 dollars
When Jack wants to buy Lear Jet
Then He doesn't have enough cash
Scenario: acquire it
Given Jack is broke
But his wealth is slowly growing
When Jack has 5 dollars
Then He can afford a pack of gum
Scenario: own it
Given Jack is broke
But he has a job that's paid 5 dollar an hour
When He works an hour
Then Jack has 5 dollars
@Given("^Jack has 5 dollars$")
public void set_it() throws Throwable {
this.jack = new Person();
jack.setCashAmount(5);
}
@When("^Jack has 5 dollars$")
public void wait_it() throws Throwable {
jack.waitFor(wealth(is(equalTo(5))));
}
@Then("^Jack has 5 dollars$")
public void check_it() throws Throwable {
assertThat("Jack should have 5 dollars by now",
jack.getCashAmount(),
is(equalTo(5)));
}
我用 cucumber 运行这个功能(用 mvn/intelliJ 插件/eclipse 插件/魔法 Crystal 球/其他可笑的媒介)
我收到以下消息:
cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in my.project.MySteps.wait_it() in file:[...] and my.project.MySteps.set_it() in file:[...]
at cucumber.runtime.RuntimeGlue.addStepDefinition(RuntimeGlue.java:33)
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:151)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:68)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:41)
at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)
...
还有一堆其他不相关的行,我会为你省去。
Cucumber 似乎在 @Given("a")
、@When("a")
和 @Then("a ")
(以及 @And
和 @But
我真的不知道它们最初的用途是什么,因为它们只是语法糖“和我刚才做的一样”)
但正如实现建议的那样,这 3 个步骤彼此截然不同。每句话都正确描述了测试应该做什么,随后的实现是明确的。
我错过了什么?
有没有办法告诉 Cucumber 如何不仅依赖正则表达式而且依赖步骤类型(对我来说,这应该是默认行为)?
最佳答案
是的,这就是它应该的工作方式。步骤名称本身应该足够清晰,可以看出它是关于测试设置 (Given)、某些操作 (When) 还是验证结果 (Then)。
Action 步骤通常可以用措辞来包含动词,以区分设置和断言我同意这有时非常困难。您必须在这里提出自己的约定,一种可能是:
Given Jack is broke
When Jack earns 5 dollars
Then Jack should have 5 dollars
关于java - Cucumber-JVM 认为所有(@Given、@When、@Then)注释都是可互换的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484528/
我是一名优秀的程序员,十分优秀!