Gherkin "OR" syntax to reduce repetition with BDD

Eamonn Boyle picture Eamonn Boyle · Feb 1, 2012 · Viewed 9.4k times · Source

Does anyone know of a way to achieve this or do they think it's a good idea. To have an OR style syntax in Gherkin for reducing repetition but maintaining human readability (hopefully). I'm thinking of cases where clause combinations are expanded with every combination of multiple OR statements. e.g.

Scenario: TestCopy
  Given Some text is selected
  When The user presses Ctrl + C
    OR the user right clicks and selects copy
    OR the user selects Edit + Copy
  Then the text is copied to the clipboard

This would run as 3 tests each with the same given and then but with one When from the OR set. I guess this could have been acheived using a template with a place holder for the When clause but I think this is more readable and could allow the OR to be used in the Given as well to produce n x m tests. With the outline you would still need n x m rows.

  • Is there a better way to do this
  • Is it better practice to explicitly copy and paste (I'm thinking maintenance could get messy)
  • Do other frameworks support this (I think with FIT you could write a custom table but again this seems like overhead)

Thanks.

Answer

nemesv picture nemesv · Feb 1, 2012

You can generatd multiple tests from one scenario with Scenario Outlines:

Scenario Outline: TestCopy
  Given Some text is selected
  When <Copy operation executed>
  Then the text is copied to the clipboard

Examples: 
    | Copy operation executed                |
    | The user presses Ctrl + C              |
    | the user right clicks and selects copy |
    | the user selects Edit + Copy           |

In a Scenario Outline you basically create a template which is filled in the with the provided Examples.
For the the above example Specflow will generate 3 tests with the same Given and Then and with the 3 different Whens:

When The user presses Ctrl + C
When the user right clicks and selects copy
When the user selects Edit + Copy