What is Gherkin syntax? Cucumber tutorial series

Updated on Sat 21 Jan, 2023 | 4 mins read | Free training, qa training, selenium


Welcome back to the Cucumber tutorial series. In the last tutorial of this series, we discussed about BDD frameworks, Cucumber, problems solved by Cucumber, etc.

To know more details, please refer to the following link:

https://www.qaonlinetraining.com/what-is-cucumber-what-are-the-bdd-frameworks-selenium-tutorials/

So far, we know what Cucumber is and what it does. It executes the test scripts, which are defined in the feature file (to be covered in subsequent tutorials). This executable feature file is written in a language known as Gherkin. Gherkin is a plain English text language that helps Cucumber interpret and run test scripts.

Gherkin is a plain English text language, but we need to follow its syntax to write a feature file. And this is something we are going to explain in this tutorial.

Let’s go…

You will learn the following points in this tutorial:

What is the Gherkin syntax?

The Gherkin language is a simple but powerful way to describe the details within your User Stories. It is meant to be easy enough for people who don’t know how to program but organized enough to show business principles in most real-world areas. Product teams use Gherkin to design tests that help them express their requirements for new products.

Important Terms used in Gherkin

Feature

The purpose of the feature is to provide a high-level description of a software feature and group-related scenarios. The feature must always be the primary keyword in a Gherkin document.
Video Tutorial

Background

The background is used to add some context to the scenario. The only difference is that it should be run before each scenario, even though it can contain some steps.

Scenario

The scenario is a core Gherkin structure. Each feature can have one or more scenarios, and each scenario can have one or more steps.

Given

This keyword is used to put the system in a familiar state before the user starts interacting with it. However, you can skip writing user interactions in Given steps if given in the ‘Precondition’ step.
Given
Given - a test step that defines the context 
Given I am on "/."

When

The WHEN clause defines the time when the Scenario starts. For example, when dealing with a website, statements list data like.
When
A When - a test step that tells you when the "action" performed. 
When I perform "Sign In."

Then

Finally, the THEN clause specifies the conditions under which the test is successful or unsuccessful. If the requirements in this phrase are met, the software will work properly; otherwise, it will fail. This result could be a computed value or any other verified conclusion.

And & But

If we have multiple Given, When, Then consecutive steps, we can use And steps. It improves the readability of the user.
But
A But - additional test step which defines the 'action' 'outcome.' 
But I should see "Welcome user."
And - additional test step that defines the 'action' performed 
And I write "Email" with "training@infotek-solutions.com."

Given, When, Then, and, but are test steps. It is possible for you to use them interchangeably. The interpreter doesn’t show any error messages. However, they will hardly make sense when read.

Best practices for using Gherkin

  • Each scenario should be executed independently.
  • Every feature should be executed.
  • The steps information should be shown in a different way.
  • Make sure your scenarios match your requirements.
  • Maintain a complete list of scenarios that should be included in a requirement document.
  • Create simple, easy-to-understand steps.
  • Make sure you check all your common scenarios.

Advantages of Gherkin

  • It is easy for non-programmers to understand.
  • For programmers, it can be a very solid base for starting their tests.
  • It makes User Stories easier to understand and understand.
  • The Gherkin script is easily understood by business executives and developers.
  • The goal of Gherkin Testing is to meet the business requirements.
  • many more

Disadvantages of Gherkin

  • It requires a lot of business involvement and collaboration.
  • It may not work in all scenarios.
  • Tests that are poorly written can easily increase the cost of testing.