BDD - Behaviour driven development

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

Some of the key things to know about BDD are -

  • Dan North created first BDD framework – Jbehave
  • Other BDD frameworks are Rbehave, Rspec, Cucumber, Behat
  • The tests are written in plain text in the form of stories or features
  • 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

  • Each feature file can contain one or more scenarios and optinally outline and background.
  • 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.

  • Given – used to specify the precondition of the scenario
  • When – used to specify the action
  • And – used to specify additional preconditions and actions
  • Then – used to specify the result of the action
  • Feature – Used to specify the description of the feature
  • Scenario – Used to specify the description of the scenario
  • Outline and Examples
  • Background – Used to specify the action to be taken before each scenario in the feature file is executed.
  • “”” – used to pass data in multi-line format
  • | – used to pass data in table format
  • @ – used to tag scenarios and features
  • # – 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.
  • In-line Variables
  • Multi-line string
  • 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. Here is the output of above example.


Web development and Automation testing

solutions delivered!!