This script serves two primary purposes:
Documents user scenarios Writing an automated test (BDD)
In this Gherkin tutorial, you will learn
What is Gherkin Language?
Why Gherkin?
Gherkin Syntax
Important Terms used in Gherkin
Gherkin Example
Best practices of using Gherkin
Advantages of Gherkin
Why Gherkin?
Before Gherkin
After Gherkin
Gherkin Syntax
Gherkin is line-oriented language just like YAML and Python. Each line called step and starts with keyword and end of the terminals with a stop. Tab or space are used for the indentation. In this script, a comment can be added anywhere you want, but it should start with a # sign. It read each line after removing Ghrekin’s keywords as given, when, then, etc.
Typical Gherkin steps look like:
Gherkin Scripts: connects the human concept of cause and effect to the software concept of input/process/output.
Gherkin Syntax:
A Gherkin document has an extension .feature and simply just a test file with a fancy extension. Cucumber reads Gherkin document and executes a test to validate that the software behaves as per the Gherkin syntax.
Important Terms used in Gherkin
Feature Background Scenario Given When Then And But Scenario Outline Examples
The naming convention is used for feature name. However, there is no set rules in Cucumber about names.
Feature:
The file should have extension .feature and each feature file should have only one feature. The feature keyword being with the Feature: and after that add, a space and name of the feature will be written.
Scenario:
Each feature file may have multiple scenarios, and each scenario starts with Scenario: followed by scenario name.
Background:
Background keyword helps you to add some context to the scenario. It can contain some steps of the scenario, but the only difference is that it should be run before each scenario.
Given:
The use of Given keyword is to put the system in a familiar state before the user starts interacting with the system. However, you can omit writing user interactions in Given steps if Given in the “Precondition” step. Syntax:
When:
When the step is to define action performed by the user. Syntax:
Then:
The use of ‘then’ keyword is to see the outcome after the action in when step. However, you can only verify noticeable changes. Syntax:
And & But
You may have multiple given when or Then. Syntax: Given, When, Then, and, but are test steps. You can use them interchangeably. The interpreter doesn’t display any error. However, they will surely not make any ‘sense’ when read.
Gherkin Examples
Example 1: The scenario mentioned above is of a feature called user login. All the words written in bold are Gherkin keywords. Gherkin will analyze each step written in the step definition file. Therefore, the steps are given in the feature file and the step definition file should match. Example 2:
Best practices of using Gherkin
Each scenario should execute separately Every feature should able to be executed along Steps information should be shown independently Connect your Scenario’s with your requirements Keep a complete track of what scenarios should be included in a requirement document Create modular and easy to understand steps Try to combine all your common scenarios
Advantages of Gherkin
Gherkin is simple enough for non-programmers to understand Programmers can use it as a very solid base to start their tests It makes User Stories easier to digest Gherkin script can easily understand by business executives and developers Gherkin Testing targets the business requirements A significant proportion of the functional specifications is written as user stories You don’t need to be expert to understand the small Gherkin command set Gherkin Test cases link acceptance tests directly to automated tests Style of writing tests cases are easier to reuse code in other tests
Disadvantages of Gherkin
It requires a high level of business engagement and collaborations May not work well in all scenarios Poorly written tests can easily increase test-maintenance cost
Summary:
Gherkin is the format for cucumber specifications Gherkin is line-oriented language just like YAML and Python Gherkin Scripts connects the human concept of cause and effect to the software concept of input/process and output Feature, Background, Scenario, Given, When, Then, And But are importantly used in Gherkin In Gherkin, each scenario should execute separately The biggest advantage of Gherkin is simple enough for non-programmers to understand Gherkin Test may not work well in all type of scenarios