Writing scenarios with Gherkin syntax

Last Updated : 22 May, 2026

Gherkin is a domain-specific language used in Behavior-Driven Development (BDD) to describe software behavior in a simple and human-readable format. It is mainly used with the Cucumber framework to write test scenarios that are easy for developers, testers, and non-technical stakeholders to understand.

  • Uses plain English syntax to describe application behavior
  • Follows the Given-When-Then structure for writing scenarios
  • Improves communication between business teams, testers, and developers

Gherkin Keywords

The Gherkin syntax is built using the following keywords:

  • Feature: Describes the feature under test.
  • Scenario: Describe a specific situation or example.
  • Given: Describes the initial context or state.
  • When: Describes the action or event.
  • Then: Describe the expected outcome or result.
  • And / But: Used to add additional conditions to the Given, When, and Then steps.

Writing Gherkin Scenarios

To write effective Gherkin scenarios follow these steps:

  • Identify the Feature: Determine the feature or functionality we want to test.
  • Define the Scenarios: The Break down the feature into the specific scenarios that cover different aspects and edge cases.
  • Write the Steps: Use Given-When-Then to the describe the steps for the each scenario.

Example

Let's consider an example of the simple login feature.

Feature: User Registration

Scenario: Successful registration with valid details

  • Given the user is on the registration page
  • When the user enters a valid username, email, and password
  • And the user clicks the register button
  • Then the user should be redirected to the welcome page
  • And the user should see a registration confirmation message

Scenario: Unsuccessful registration with invalid email

  • Given the user is on the registration page
  • When the user enters a valid username and password, but an invalid email address
  • And the user clicks the register button
  • Then the user should see an error message indicating an invalid email

Scenario: Unsuccessful registration with missing fields

  • Given the user is on the registration page
  • When the user leaves the username and email fields blank
  • And the user clicks the register button
  • Then the user should see a validation error message indicating required fields

Best Practices for Writing Gherkin Scenarios

  • Keep it Simple: Write clear and concise scenarios. To Avoid technical jargon and complex sentences.
  • Focus on Behavior: The Describe the behavior of the system from user's perspective not the implementation details.
  • Use Consistent Language: The Maintain consistency in the language and terminology used across the all scenarios.
  • Avoid Multiple Actions: Each When step should represent a single action to keep scenarios focused and easy to the understand.
  • Collaborate with Stakeholders: Involve all relevant stakeholders in the writing and reviewing scenarios to the ensure they accurately reflect the desired behavior.

Advanced Gherkin Features

  • Background: Used to define common Given steps that are shared across the multiple scenarios within a feature.
  • Scenario Outline: The Allows to run the same scenario multiple times with the different sets of data.

Example with Background and Scenario Outline

Feature: User Registration

Background: Given the user is on the registration page

Scenario Outline: Unsuccessful registration with various invalid inputs

  • When the user enters <username>, <email>, and <password>
  • And the user clicks the register button
  • Then the user should see a validation error message

Examples:

  1. | username | email | password |
  2. | | invalid@example.com | validPass |
  3. | validUser | invalid-email | validPass |
  4. | validUser | valid@example.com | short |
  5. | validUser | valid@example.com | |

Tools for Writing and Running Gherkin Scenarios

  • Cucumber: A popular tool that supports BDD and runs Gherkin scenarios.
  • SpecFlow: A BDD tool for the .NET that supports Gherkin syntax.
  • Behave: A BDD tool for the Python that uses Gherkin.
Comment
Article Tags:

Explore