BDD software development with Gherkin

BDD is an emerging software development model used along with agile methodologies.

Some of the key things to know about BDD are –

  1. Dan North created first BDD framework – Jbehave
  2. Other BDD frameworks are Rbehave, Rspec, Cucumber, Behat
  3. The tests are written in plain text in the form of stories or features
  4. The Domain specific language (Gherkin) is used for writing the tests.

What is Gherkin?

Gherkin is the plain text language used to write feature files (also called as story files) in BDD frameworks like cucumber. Gherkin is implemented in most of languages in the world like English, German, French….many more. This means that any project stakeholder can write, read and understand the tests written in Gherkin. That’s the crux of the Gherkin language.

Key points to note about feature file

  1. Each feature file can contain one or more scenarios and optinally outline and background.
  2. Each scenario has steps like given, when, and, then, but

What are the keywords in Gherkin?

Gherkin has reserved some keywords which have specific meaning in the context of feature files.

Below is the list of keywords in Gherkin.

  1. Given – used to specify the precondition of the scenario
  2. When – used to specify the action
  3. And – used to specify additional preconditions and actions
  4. Then – used to specify the result of the action
  5. Feature – Used to specify the description of the feature
  6. Scenario – Used to specify the description of the scenario
  7. Outline and Examples
  8. Background  – Used to specify the action to be taken before each scenario in the feature file is executed.
  9. “”” – used to pass data in multi-line format
  10. | – used to pass data in table format
  11. @ – used to tag scenarios and features
  12. #  – used to comment the line in feature file

Below feature file will make you understand most of the syntax of Gherkin language.

Basic Sample feature file

Now let us take a look at simplest feature file that we can have in gherkin.

Feature: ATM Card authentication
    User should be asked for the PIN once ATM card is inserted into the ATM machine.
    User should be given warning message if PIN number entered is wrong.
User should be authenticated successfully if PIN number entered is correct.

  Scenario: Enter wrong PIN at ATM
    Given I insert the ATM card in ATM machine
    And I am asked to enter the PIN
    When I enter the correct PIN
    Then I should be able to see option to withdraw money

Sample feature file with Outline and Examples keyword

Scenario outline are used in cases where we need to repeat same set of actions but with different combination of data. For instance, In below scenario outline, we need to test the login functionality with different combinations of user id and password. In such cases, we use <VariableData> syntax to specify the varying data and it is supplied through examples keyword. So in below scenario, all steps will be executed 4 times – one for each user.

Scenario Outline: Password rules
  Given that we are on login page
  When we enter user id <ID> and password <PASSWORD>
  Then the login should be <OUTCOME> 
  
  Examples:
    | ID     | PASSWORD | OUTCOME |
    |   Sagar|  128$9#  |     Fail|
    |   Amol |  98sjdg  |     PASS|
    |  Shaun |  jslsh9  |     PASS|
    |    Ben |  KON()%  |     FAIL|

 

Sample feature file with Background

Background keyword is used when we want to execute certain number of steps before each scenario. For example, suppose you want to clear browser cookies before execution of each scenario, then we can use background keyword with step to clear cookies.

Passing input data to the step

Each scenario can have multiple steps and each step is the user action. A step is like specific operation in the system. A steps may need the input data to be processed. We can pass the input to the step using 3 ways.

  1. In-line Variables
  2. Multi-line string
  3. Tables

Grouping the scenarios and features with tags

We use tags to group similar scenarios and features together in Gherkin. @ symbol is used for tagging the scenarios and features.