gpt4 book ai didi

java - Cucumber-JVM 认为所有(@Given、@When、@Then)注释都是可互换的

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:47 27 4
gpt4 key购买 nike

我已经(非常残酷地)注意到,对于 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 个步骤彼此截然不同。每句话都正确描述了测试应该做什么,随后的实现是明确的。

  • “给定”步骤应设置测试先决条件
  • “何时”步骤应该触发一个 Action 或等待一个 Action
  • “Then”步骤应该在之后断言系统的状态。

我错过了什么?

有没有办法告诉 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com