Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gherkin "OR" syntax to reduce repetition with BDD

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.

like image 289
Eamonn Boyle Avatar asked Feb 01 '12 09:02

Eamonn Boyle


People also ask

What are reasons for choosing to use the Gherkin syntax?

This syntax promotes behavior-driven development because it allows developers, managers, business analysts and other parties involved to understand the requirements of the project and the life-cycle. The language makes it easy to create simple documentation of the code that's being written.

Is gherkin a BDD?

Gherkin is part of the Cucumber framework for BDD, which aims to create a testing framework in easily readable language based on a few keywords.

Which syntax is used by the Cucumber scenario?

Gherkin Syntax - Cucumber Documentation.

What is the difference between gherkin and Cucumber language?

Gherkin is Cucumber's language parser, which allows software behaviours to be specified in a logical language that people can understand. This means that Cucumber feature documentation is written in business-facing text that is non-technical and human readable for stakeholders like business analysts and managers.


2 Answers

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
like image 100
nemesv Avatar answered Oct 13 '22 23:10

nemesv


It's not recommended to use this detail level (pressing these keys, right clicking) on the scenarios. This makes them, as you realize, lengthy and repetitive. Also, that's usually not the information the stakeholders would need or want anyway.

The best would be to hide the implementation details on the step definitions. Your scenario would be something like:

Scenario: TestCopy
  Given some text is selected
  When the user copies the selected text
  Then the selected text is copied to the clipboard

And the different ways of copying the text would go to the 3rd step definition.

like image 37
Marcelo Diniz Avatar answered Oct 14 '22 00:10

Marcelo Diniz