【问题标题】:Specflow: Specifying multiple scenarios per featureSpecflow:为每个功能指定多个场景
【发布时间】:2014-02-12 10:55:28
【问题描述】:

通过 SpecFlow 文档,我试图弄清楚我的看法是否错误。我想为每个功能指定几个完全不同的场景。

例如:

Feature: Serve coffee
    Coffee should not be served until paid for
    Coffee should not be served until the button has been pressed
    If there is no coffee left then money should be refunded

  Scenario: Buy last coffee
    Given there are 1 coffees left in the machine
    And I have deposited 1$
    When I press the coffee button
    Then I should be served a coffee

如果我想查看“提供咖啡”功能中的其他场景怎么办?例如,支付了钱但 5 分钟未按下按钮的场景。

有几个场景有意义还是我应该使用场景大纲?

谢谢!

【问题讨论】:

  • 我认为您有这些疑问的原因是由于您对功能的定义非常广泛。尝试使用As a ... I want ... So that ... 模式来描述您的功能,您会发现它有助于保持您的场景分离

标签: c# bdd specflow gherkin


【解决方案1】:

每个功能的多个场景都可以,只要它们在逻辑上位于同一区域即可。如果您尝试解决不同的用例,我可能会建议将其作为新功能。在您的情况下,这两种情况似乎可以很好地适应相同的功能。

Scenario Outline 类似于 NUnit 中的TestCase,只有在相同的场景结构只需要采用不同的参数时才使用它。


SpecFlow github 站点在 Scenario Outlines here 上有很好的文档。我将总结下面的代码示例。

给定一个功能的两个场景:

Scenario: eat 5 out of 12
  Given there are 12 cucumbers
  When I eat 5 cucumbers
  Then I should have 7 cucumbers

Scenario: eat 5 out of 20
  Given there are 20 cucumbers
  When I eat 5 cucumbers
  Then I should have 15 cucumbers

您可以使用轮廓参数化重复部分:

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |  12   |  5  |  7   |
    |  20   |  5  |  15  |

此大纲替换了您尝试参数化的场景定义。

【讨论】:

  • 你能指导我如何做到吗?我发现只有一个场景的文档或使用场景大纲的“多个”。谢谢!
  • @Nir 只需指定一个新的Scenario: 标签即可添加多个场景。场景大纲在其站点上的 SpecFlow 文档中有详细说明。如果没有真正了解您要参数化的内容,我将无法提供代码示例。给我几分钟看看。
  • 谢谢亚当,我会尝试更具体一点:如果我想添加一个场景,即支付了钱但 5 分钟内没有按下按钮,这被认为是不同的功能还是不同的场景?
  • @Nir 查看您的功能定义的不同场景。
【解决方案2】:
Feature: Serve coffee
  Coffee should not be served until paid for
  Coffee should not be served until the button has been pressed
  If there is no coffee left then money should be refunded

Scenario: Buy last coffee
  Given there are 1 coffees left in the machine
  And I have deposited 1$
  When I press the coffee button
  Then I should be served a coffee

Scenario: Store credit until a coffee is selected
  Given I have deposited 1$
  And I have left the machine for 5 minutes
  When I press the coffee button
  Then I should be served a coffee

您要问的是在功能文件中使用 specflow 场景的标准方式。所以答案是“是的,将与特定‘功能’相关的场景(在这种情况下,咖啡的供应时间和方式)放在单个功能文件中”。

如果您的咖啡机功能文件扩展为具有描述完全不同功能的额外场景,则将它们移动到不同的文件中。

例如

Feature: Coffee Machine Advertising video panel

Scenario: While my coffee is being served, I should be shown a 15 second advert.

【讨论】:

  • 谢谢。我已经尝试将几个场景放在一个功能文件中,但 SpecFlow 不会为超过 1 个场景生成规范定义。
  • Specflow 不允许多个Feature 标签,但允许在一个文件中包含多个Scenario 标签。
猜你喜欢
  • 2018-03-10
  • 2021-08-24
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多