黄瓜步骤定义是全球性的吗?

Nar*_*esh 5 cucumber cucumberjs

我只是在学习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
Run Code Online (Sandbox Code Playgroud)

经纪人写道

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
Run Code Online (Sandbox Code Playgroud)

请注意,两个方案的When子句相同.更糟糕的是,两个人都把这个场景放在一个名为transaction-fees.feature的文件中(当然在不同的目录中).

黄瓜为步骤定义提出以下建议:

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();
});
Run Code Online (Sandbox Code Playgroud)

请注意,when子句仅建议一次.

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

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

Jef*_*ice 3

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

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

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

  • 谢谢 - 现在很清楚了!让我想知道如何在大型项目中扩大规模而不互相绊倒。 (2认同)