How to Avoid Duplication of Scenarios in Feature File.


In general, Copying and pasting scenarios which has different values but same scenario is waste of time.In order to minimize this, Scenario outline could be used.

For example : These two scenarios are similar but having different values.

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 Outlines allow us to more concisely express these examples through the use of a template with placeholders.
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  |

The Scenario outline steps provide a template which never runs directly. A Scenario Outline will run once for each row.The Scenario Outline uses placeholders, which contains anything present in  ‘< >’ in the Scenario Outline’s steps.This will always skip the first row.So the first row should be headings. In this example ‘start’ ‘eat’ ‘left’ are headings.

For example:

Given <I'm a placeholder and I'm ok>

So by using this method we can avoid duplication of writing scenarios in feature files.Cucumber turns each example—each table row—into a concrete scenario before looking for matching step definitions.In most cases, I recommend starting with concrete scenarios and refactoring to scenario outlines once you have a few scenarios working with the correct step definitions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s