5

我只是在学习 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 子句。

  1. 这是否意味着只需要在两个步骤定义文件之一中输入一个步骤定义?
  2. cucumber 是否将特征文件与名称相似的 step_definition 文件相关联?换句话说,它是否将 transaction-fees.feature 与 transaction-fees.steps.js 相关联?如果所有步骤定义都是全局的,那么我可能会错误地认为文件/目录设置仅用于组织,就执行环境而言没有任何意义。

提前感谢您的时间和澄清。

4

1 回答 1

3

步骤定义附加到 World 对象,即上面代码中的“this”。

  1. 应该只有一个步骤定义。它们是用来共享的。IIRC,Cucumber Boo,第 149 页 ( https://pragprog.com/book/hwcuc/the-cucumber-book ) 详细介绍了此设计决策。虽然它是 ruby​​,但我认为这在所有 cucumber 实现中都是相同的。

  2. Cucumber 不关联特征文件和 step_definition 文件。文件树/约定只是为了方便。

于 2014-10-03T15:31:10.563 回答