Gherkin Syntax: What is Gherkin and its uses with Cucumber

So far, we have discussed a lot about Behaviour driven development and its tool which is known as Cucumber. In cucumber introductory tutorial we have discussed that there is a feature file which is the resultant of the consensus of all the project’s stakeholders like Testers, Developers, Business Analyst, Product Owner, and Client. We also discussed that we use Gherkin syntax to explain the test steps in an explanatory form. Basically, Gherkin syntax helps to clarify the action behind the test steps. Thus, through this article, I am going to make the knowledge of Gherkin stronger so that you should avoid any confusions while designing your Cucumber feature file.

Recommended Reading: How to install Cucumber in Eclipse?

Let’s start!

What is Gherkin?

Gherkin is a plain English based language which is used to write test cases or feature or scenarios in Cucumber. Anyone can easily recognize and understand the language. Gherkin helps us to describe the business logic in a simple manner, without digging into its implementation.

It is a light-weight language which helps to document the behaviour of the application on demand of business stakeholders.

Not only English, but Gherkin can also be written in more than 60 spoken languages like Spanish, French, Italian, Arabic and Norwegian.

Why do We need Gherkin?

A development team consists of Architect, Developer, and Tester. Whereas, A business team consists of Business Analyst, Product Owner, and Delivery Managers. Thus, during the development, deployment, and release of the software, the development team and business teamwork in collaboration.

Here, the business team brings requirement and give it to the development team for its development. Don’t you think the situation will be tough for the business team to understand the technical implementation? Hence, to overcome such Gap, we use Cucumber with Gherkin altogether.

Gherkin SyntaxWhat is Gherkin Syntax?

Gherkin is the simple, lightweight and structured language which is spoken in daily activities. Therefore, Gherkin syntax is a line-oriented language which means indentation used to define the structure. Here each line in Gherkin syntax acts as the steps which start with a keyword.

Keywords used in Gherkin Syntax

Here are some the keywords which are used to design the feature and a complete Gherkin syntax.

  • Feature
  • Scenario
  • Background
  • Given
  • When
  • Then
  • And
  • But
  • #
  • *
  • Examples
  • Scenario Outline

We will discuss the uses of these keywords in a tutorial where we will discuss on cucumber feature design.

Comments in Gherkin syntax

We use # (hash) to write a comment anywhere in the feature file. So, the Gherkin syntax of the comment will be like-

#This is a comment line

Comments are used to give the additional details of the feature, scenario and test step.

Parameters in Gherkin syntax

We can insert parameters in test steps and parameters are enclosed within double quotes – “ “.

Cucumber Gherkin SyntaxData tables in Gherkin syntax

We define data tables when we have to execute the same step for multiple test data. Data in the data table are separated by the pipe ‘|’ symbol. One point you must keep in mind that the columns of the first row will always be selected as the column heading by default.

Here is the example of Data tables:

|Username|Password|
|myuser|mypass|
|Avinash|test|

Here Username and Password will be the column title. This technique is used to design data driven framework with Cucumber.

Example of Gherkin

Feature: Login Gmail

Scenario: Successful login to Gmail account
Given I am a Gmail user
When I enter the username
And I enter the password
Then I should be login to my Gmail account

This is all about using Gherkin Syntax to design your feature files in the Cucumber BDD tool. You can post your queries in the comment below.

Join Inviul fb group

One Response

Leave a Reply