我正在学习Cucumber,并注意到如果两个完全独立的功能有两个意外措辞相同的步骤,Cucumber建议他们只有一个步骤定义。这是否意味着步骤定义是全局的并且它们是要共享的?
假设一个业务分析师团队正在为一家拥有银行部门和经纪部门的金融公司编写规范。进一步假设两个不同的人正在为各自的部门编写功能来计算交易费用。
银行家伙写道:
Feature: Transaction Fees
Scenario: Cutomer withdraws cash from an out-of-netwrok ATM
Given that a customer has withdrawn cash from an out-of-netwrok ATM
When I calculate the transaction fees
Then I must include an out-of-netwrok ATM charge
经纪人写道
Feature: Transaction Fees
Scenario: Cutomer places a limit order
Given that a customer has placed a limit order
When I calculate the transaction fees
Then I must include our standard limit-order charge
请注意,两个方案的When子句相同。更糟糕的是,两个人都将这个场景放在一个名为transaction-fees.feature的文件中(当然在不同的目录中)。
Cucumber为步骤定义提出以下建议:
You can implement step definitions for undefined steps with these snippets:
this.Given(/^that a customer has withdrawn cash from an out\-of\-netwrok ATM$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.When(/^I calculate the transaction fees$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Then(/^I must include an out\-of\-netwrok ATM charge$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Given(/^that a customer has placed a limit order$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Then(/^I must include our standard limit\-order charge$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
请注意,when子句仅建议一次。
提前感谢您的时间和澄清。
答案 0 :(得分:2)
步骤defs附加到World对象,这是"这个"在上面的代码中。
应该只有一个步骤定义。它们是为了共享。 IIRC,Cucumber Boo,第149页(https://pragprog.com/book/hwcuc/the-cucumber-book)详细介绍了这一设计决定。虽然它是红宝石,但我认为这在所有黄瓜实施中都是一样的。
Cucumber不会关联要素文件和step_definition文件。文件树/约定仅为方便起见。