Specflow: Specifying multiple scenarios per feature

Nir picture Nir · Feb 12, 2014 · Viewed 10.8k times · Source

Going through SpecFlow docs, I'm trying to figure out if my perception is wrong. I would like to specify several completely different scenarios per feature.

For example:

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

What if I want to check other Scenarios in the "Serve coffee" feature? For example, a scenario where the money was paid but the button was not pressed for 5 minutes.

Does it make sense to have several scenarios or should I use a scenario outline?

Thanks!

Answer

Adam Houldsworth picture Adam Houldsworth · Feb 12, 2014

Multiple scenarios per feature is fine, so long as they are logically in the same area. If you are attempting to address a different use case I would probably suggest making it a new feature. In your case, it looks like the two scenarios would fit fine under the same feature.

Scenario Outline is analogous to TestCase in NUnit, you would only use this if the same scenario structure just needs to take different parameters.


The SpecFlow github site has good documentation on Scenario Outlines here. I will summarise the code sample below.

Given two scenarios in a feature:

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

You can parameterise repeating parts using an outline:

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  |

This outline replaces the scenario definitions you are trying to parameterise.