All IT Courses 50% Off
QA Tutorials


Gherkin is a domain particular language which helps you to describe business behaviour without any need for detail implementation. The text acts as documentation and skeleton of our automated tests.

This script has two main purposes:

  • Documents user scenarios
  • Writing automated tests (BDD)

Gherkin Syntax:

Gherkin is line-oriented language like YAML and Python. Here every line is called as step and starts with a keyword and end terminals with stop. In this syntax the comment can be added anywhere you want, but it will start with a # sign reads each line when gherkin’s keywords is removed as given, when, then etc.

Example the script is written as:

Feature: Title of the Scenario
Given [Preconditions or Initial Context]
When [Event or Trigger]
Then [Expected output]

The important terms of gherkin are:


  • Background
  • Scenario
  • Given
  • When
  • Then
  • And
  • But
  • Scenario outline examples


The file has an extension .feature and each feature file must have only one feature the feature keyword starting with Feature and after that add, a space and name of the feature is written.


The feature file may have many scenarios will start with Scenario: followed by scenario name.


Background keyword helps us to add some content to the scenario, but the scenario can be included in the background and the difference is that it should run before scenario.


The main usage of this keyword is to put the system in a familiar state before user interaction starts with the system. 

The syntax:


Given - a test step that defines the 'content’
Given I am on "/."

This step is to define action performed by user.


When I perform “Sign In.”


This keyword is to see the outcome after the action in when step.


Then - test step that defines the 'outcome.'
Then I should see "Welcome Tom."

And and But:

Used when we use multiple given, then and when commands. All these are test steps and can be used interchangeably



A But - additional test step which defines the 'action' 'outcome.'
But I should see "Welcome Tom."
And - additional test step that defines the 'action' performed
And I write "Email Address" with ""

Example of Gherkin is:

Feature: User Authentication Background:
Given the user is already registered to the website Scenario:
Given the user is on the login page
When the user inputs the correct email address
And the user inputs the correct password
And the user clicks the Login button
Then the user should be authenticated
And user should be redirected to their dashboard
And the user should be presented hit message

Advantages of Gherkin:

  1. Gherkin is simple easy for non-programmers to understand
  2. Programmers can use it as a solid base for starting their tests
  3. By using gherkin user stories can be digested easily.
  4. The scripts of gherkin can be easily understood by business executives and developers.
  5. It targets the business requirements.
  6. A significant proportion of the functional specifications are used as user stories.
  7. Expertise is not necessary to understand the small Gherkin command set
  8. The Gherkin language links acceptance tests directly to automated tests.
  9. Style of writing tests is easier for code reusability in other tests


1. It needs a high level business engagements and collaborations.

2. It is not good or works well with all scenarios.

3. Test maintenance costs is increased due to poorly written tests

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Check Also
Back to top button

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.