Skip to main content
Logo
    Generic selectors
    Exact matches only
    Search in title
    Search in content
    Post Type Selectors

Gherkin Language: Format, Syntax & Gherkin Test in Cucumber

Gherkin Syntax

04 Apr 2025

Read Time: 3 mins

The Gherkin language is a simple text format that BDD uses to formulate test cases so technical teams and business stakeholders can relate more easily to understand the tests. It serves as the basis for frameworks such as Cucumber, which enable teams to write tests based on expected behaviors instead of technical details.

This Cucumber Gherkin tutorial will help beginner understand the Gherkin syntax, guiding them to write steps for a Behavior-Driven Development (BDD) test case in Gherkin and insights for teams to smooth out their testing processes.

What Is Gherkin?

Gherkin is a DSL (Domain Specific Language) language for writing test cases for software applications. With Gherkin, one can describe the behavior of a software system in an easily read structured format that is very close to normal language. It is designed to serve as a common format that allows business stakeholders to understand the behavior of the system and get on the same page with domain experts.

Key features of Gherkin include:

  • Plain Text Syntax: Easily readable by all team members, regardless of technical expertise.
  • Behavior Focus: Emphasizes what the system does rather than how it works.
  • Multilingual Support: Allows test scenarios to be written in over 60 languages.
  • Executable Specifications: Each Gherkin scenario is directly linked to automation steps, making it executable.

Gherkin Syntax For Beginners With Structure

The syntax of Gherkin is developed to be both simple and expressive so that one can use natural language in the description of system behavior.

1. Feature

The Feature keyword is used to group related scenarios. It describes the high-level functionality being tested and provides context for the scenarios it contains. A feature can be considered a user story or a requirement.

Example:

Feature: User Login

  • This feature allows users to log in to the application.

2. Scenario

The Scenario keyword describes a specific situation or test case. Each scenario represents a single path through the feature, focusing on a particular aspect of the functionality.

Example:

Scenario: Successful Login

  • Given the user navigates to the login page
  • When the user enters valid credentials
  • Then the user is redirected to the homepage

3. Given

The Given keyword sets up the initial context or preconditions for the scenario. It describes the state of the system before the main action occurs.

Example:

  • Given the user navigates to the login page

4. When

The When keyword specifies the action or event that triggers the scenario. It describes what the user does or what happens in the system.

Example:

  • When the user enters valid credentials

5. Then

The Then keyword states the expected outcome of the scenario. It describes the expected result of the action or event.

Example:

  • Then the user is redirected to the homepage

6. And/But

The And and But keywords are used to add additional steps or conditions to the scenario. They can be used in combination with Given, When, and Then to make the scenario more descriptive.

Example:

  • Given the user navigates to the login page
  • And the login page is displayed
  • When the user enters valid credentials
  • And clicks the login button
  • Then the user is redirected to the homepage
  • But an error message is not displayed

Writing Scenarios With Gherkin

Here is a detailed example of how to write test scenarios using Gherkin:

Feature: User Login

This feature allows users to log in to the application.

Scenario: Account Locked After Multiple Failed Attempts

  • Given: the user navigates to the login page
  • And: the login page is opened
  • When: the user enters incorrect credentials three times
  • Then: the user’s account is locked an error message is shown indicating the account is locked
  • And: instructions to unlock the account are provided

Scenario: Successful Account Unlock After Lockout

  • Given: the user’s account is locked due to multiple failed login attempts
  • When: user navigates to the account unlock page
  • And: enters the correct email associated with the account
  • And: follows the provided instructions
  • Then: the user’s account is unlocked
  • And: the user can log in with credentials

Gherkin Test in Cucumber

Cucumber is one of the widely used BDD testing tools that bring scenarios defined in Gherkin to reality, easily integrating Gherkin and Cucumber testing framework so that the feature files being automated can be assured of having executable test cases.

Here’s how Gherkin and Cucumber work together:

  • Feature Files: Written in Gherkin, these files define the behavior in plain text.
  • Step Definitions: Each Gherkin step is mapped to automation code in Cucumber, which executes the corresponding actions.
  • Execution and Reporting: Cucumber runs the scenarios and provides detailed feedback on test outcomes.

This integration ensures that the Gherkin scenarios act as living documentation, always aligned with the code and business requirements

When comparing Cucumber testing vs ACCELQ – ACCELQ takes a different approach by leveraging AI-powered codeless automation. It provides a natural language interface and visual automation capabilities, enabling seamless test management and execution as part of broader test automation strategies.

Perform cucumber testing and streamline your testing process.

[Try ACCELQ now!]

Best Practices For Writing BDD Test Cases in Gherkin

To create effective Gherkin tests, focus on clarity and collaboration:

  • Define Features Clearly: Use descriptive titles that outline the feature’s purpose.
  • Write Scenarios Collaboratively: Involve stakeholders to ensure scenarios align with business expectations.
  • Keep Steps Simple: Write steps that are easy to understand and reusable across scenarios.

Example:

Feature: Product Search

  • Given the user is on the search page
  • When he user enters the product name
  • Then the product should appear in the search results

By emphasizing readability and behavior, Gherkin test cases ensure all team members can contribute to and benefit from the testing process.

Conclusion

In this Cucumber Gherkin tutorial, we learned how the Gherkin language, when used with Cucumber, makes BDD implementation much easier by converting plain-text scenarios into executable tests. Integration of Gherkin tests in Cucumber helps teams automate their feature files efficiently and closes the gap between technical and business stakeholders.

Gherkin is an essential tool for BDD testing providing simplicity and collaboration. Collaboration platforms such as ACCELQ elevate automation by following a design-first approach, allowing teams to define, manage, and execute test scenarios using natural English language. This simplifies test automation and ensures scalability and efficiency for organizations. Whether you are picking up Gherkin syntax for beginners or sharpening your skills with a Cucumber Gherkin tutorial, Gherkin and its ecosystem are essential for modern software development.

Geosley Andrades

Director, Product Evangelist at ACCELQ

Geosley is a Test Automation Evangelist and Community builder at ACCELQ. Being passionate about continuous learning, Geosley helps ACCELQ with innovative solutions to transform test automation to be simpler, more reliable, and sustainable for the real world.

You Might Also Like:

Playwright for Cross-Browser TestingBlogTestingTypes of TestingPlaywright Cross-Browser Testing: Key Challenges & Solutions
7 January 2025

Playwright Cross-Browser Testing: Key Challenges & Solutions

Discover how Playwright simplifies cross-browser testing. Learn key features, challenges, and best practices for seamless app compatibility.
Website testing checklists-ACCELQBlogTestingWant a Flawless Website? Follow this Website QA Checklist!
10 February 2025

Want a Flawless Website? Follow this Website QA Checklist!

Ensure your website is secure, bug-free, and high-performing with ACCELQ's automated testing checklist for seamless QA.
How to achieve guarnteed roi with salesforce testing-ACCELQBlogTestingHow To Achieve Guaranteed ROI With Salesforce Testing?
16 August 2022

How To Achieve Guaranteed ROI With Salesforce Testing?

To enable maximum ROI from Salesforce testing, enterprises must keep up with the best practices of Salesforce testing, listed here.

Get started on your Codeless Test Automation journey

Talk to ACCELQ Team and see how you can get started.