Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow: Specifying multiple scenarios per feature

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!

like image 350
Nir Avatar asked Feb 12 '14 10:02

Nir


2 Answers

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.

like image 166
Adam Houldsworth Avatar answered Sep 30 '22 17:09

Adam Houldsworth


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

What you are asking about is the standard way of using specflow scenarios in a feature file. So the answer is "yes, put scenarios related to a particular 'feature' (in this case when and how coffee is served) in a single feature file".

If your coffee machine's feature file expands to have of extra scenarios that appear to describe quite distinct functionality, then move them into different files.

e.g.

Feature: Coffee Machine Advertising video panel

Scenario: While my coffee is being served, I should be shown a 15 second advert.
like image 36
perfectionist Avatar answered Sep 30 '22 16:09

perfectionist