【问题标题】:Gherkin Multiple step definitions matchGherkin 多步骤定义匹配
【发布时间】:2019-10-16 12:27:09
【问题描述】:

我正在努力了解 Gherkin,足以让我能够分离出用户故事并让业务专家编写它们。

如果我有一个背景(版本 1),这是一个常见的前提条件,为什么我的所有场景都会收到错误消息,告诉我“多步骤定义匹配”,这不就是背景的意义,对所有人来说都是通用的吗?

如果我有不同的 Given 语句(版本 1),那么后面的 When 是否应该允许基于不同的起始位置执行相同的操作?同样,When 会导致“多个步骤定义匹配”

这是我的功能文件。理想情况下,我想要版本 1,这就是业务专家编写它们的方式,分开,易于阅读,但在没有“多步骤定义匹配”错误的情况下使其正常工作的唯一方法是版本 2,其中每个 When 需要组合,更复杂,更难阅读。

为什么我不能在超过 1 个场景中使用背景?

这些强制更改很臭,所以我做错了什么,我错过了什么?我找到了数百个简单的例子,但我觉得我缺少某种 Gherkin 的指导原则。我错过了什么?

Feature: Help
    When users says help
    "As a user
    I want the bot to understand me when I ask for help, 
    In order that I get some guidance, or some idea what to do next"

  # Version 1
    Background: 
      Given the user is in conversation with the bot
      *// above line causes error*

    Scenario: No topic
        Given there is no topic
         When the user says help
         *// above line causes error*
         Then the bot responds with a sorry, regretful message
          And then asks if the user would like to see a list of available features

    Scenario: A valid topic
        Given there is a valid topic
         When the user says help
         *// above line causes error*
         Then the bot responds with a confirmation message
          And then asks if the user would like to see a list of topic features

  # Version 2
    # Scenario: All
    #     Given the user is in conversation with the bot
    #      When the user says help and there is no topic
    #      Then the bot responds with a sorry, regretful message
    #       And then asks if the user would like to see a list of available features
    #      When the user says help and there is a valid topic
    #      Then the bot responds with a confirmation message
    #       And then asks if the user would like to see a list of topic features


Failures:

1) Scenario: No topic # features/help.feature:10
   ✖ Given the user is in conversation with the bot
       Multiple step definitions match:
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:4 
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:21
   - Given there is no topic # tests/feature_definitions/help_definition.js:7
   ✖ When the user says help
       Multiple step definitions match:
         the user says help - tests/feature_definitions/help_definition.js:10
         the user says help - tests/feature_definitions/help_definition.js:27
   - Then the bot responds with a sorry, regretful message # tests/feature_definitions/help_definition.js:13
   - And then asks if the user would like to see a list of available features # tests/feature_definitions/help_definition.js:16

2) Scenario: A valid topic # features/help.feature:16
   ✖ Given the user is in conversation with the bot
       Multiple step definitions match:
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:4 
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:21
   - Given there is a valid topic # tests/feature_definitions/help_definition.js:24
   ✖ When the user says help
       Multiple step definitions match:
         the user says help - tests/feature_definitions/help_definition.js:10
         the user says help - tests/feature_definitions/help_definition.js:27
   - Then the bot responds with a confirmation message # tests/feature_definitions/help_definition.js:30
   - And then asks if the user would like to see a list of topic features # tests/feature_definitions/help_definition.js:33

【问题讨论】:

  • 有多个步骤定义,其正则表达式匹配每个场景中的一个或多个步骤。您使用哪种语言来实施步骤?好像你有两种方法来实现When the user says help
  • 我正在使用 cucumber-js。必须在全局范围内唯一地描述所有内容似乎非常有限制。您是否花费所有时间向业务用户解释他的用户故事需要以不同的方式编写,因为我无法在不更改与之匹配的小黄瓜功能描述的情况下更改黄瓜定义,而且我不是在编写小黄瓜功能.
  • 如果同一步骤需要两个不同的定义,那么听起来您需要两个不同的测试项目。您是否分别为 Web 应用程序和 Web API 编写黄瓜测试?
  • 请包括您用于这些步骤的步骤定义。

标签: javascript bdd gherkin cucumberjs


【解决方案1】:

您的步骤有多个定义。 Cucumber 每一步只允许一个步骤定义。根据您的描述,您似乎为每个场景提供了一个步骤定义,因此这两个场景都有自己的When the user says help 定义。这是定义步骤定义的错误方法。每个步骤应该只有一个定义,因此您会得到错误。

When the user says help 的定义应适用于使用此步骤的所有 场景。它的定义应该是通用的。

如果同一步骤确实需要根据场景有所不同,您有两种选择:

  1. 创建一个单独的测试项目,这样您就可以拥有 2 个全局唯一定义,因为您将有 2 个单独的全局上下文用于步骤定义。

  2. 参数化步骤

参数化步骤可能是最简单的事情,我将以When the user says help为例:

Scenario: No topic
    Given there is no topic
     When the user says "help"
     Then the bot responds with a sorry, regretful message
      And then asks if the user would like to see a list of available features

上面的行将单词help 放在双引号内。生成步骤定义将允许您将引号之间的文本作为参数传递给您的步骤定义方法:

When(/the user says "([^"]+)"/, function(textToSay) {
    // "speak" to the chat bot using whatever API you have defined
    chatBot.receiveMessage(textToSay);
});

现在这一步可以接受多条文本:

When the user says "help"
...

When the user says "list topics"
...

When the user says "I WANT TO SPEAK TO A REAL PERSON!!!!"
Then the bot responds with "I'm sorry, Dave. I'm afraid I can't do that."

(好吧,我做了最后一步,但如果机器人不理解用户在说什么,这将是一个有趣的响应)

【讨论】:

    【解决方案2】:

    感谢大家的回答。我也去过黄瓜松弛支持频道,在所有这些想法中,一分钱终于掉了。

    我读过的每个示例都显示了与步骤定义文件一对一映射的功能文件。

    在我看来,这些功能在终端窗口中创建了模板,然后我将其放入 step.js 文件中,这些文件不是在测试中运行的文件,它们毕竟是 .js!终端模板是重复的,我将它们功能场景复制到 steps.js 1-to-1

    我没想到功能文件仍然是过程的一部分,实际上是测试运行时的步骤驱动程序。

    我现在将我的步骤组织在一个完全不同的文件夹文件结构中,不再与功能文件是一对一的,我没有看到任何示例表明,用户说什么,机器人响应什么,主题等等...,现在这一切都变得更有意义了,当然,它已经删除了所有重复的步骤。

    再次感谢所有将想法推向正确方向的人:-)

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 2018-06-20
      相关资源
      最近更新 更多