Gherkin
Gherkin is a simple, human-readable language used to write behavior specifications in Behavior-Driven Development (BDD). It uses a structured format with keywords like Given, When, and Then to describe software features and scenarios, making them understandable to both technical and non-technical stakeholders.
First used·2007
Definitions·1
Synonyms·3
Category·Software Development
Also known as
Definitions
What it means.
- 01
Gherkin in Behavior-Driven Development (BDD)
Gherkin is a business-readable, Domain-Specific Language (DSL) designed to describe software's behavior without detailing its implementation. It is a cornerstone of Behavior-Driven Development (BDD), acting as a bridge between business requirements and technical execution.
The primary goal of Gherkin is to foster collaboration among team members, including developers, testers, business analysts, and product owners. By using a plain language format, it ensures that everyone has a shared understanding of the system's intended behavior, creating what is often called "living documentation."
Key Structure and Keywords
Gherkin syntax is built around a set of keywords that provide a clear and consistent structure to behavioral specifications, which are saved in files with a
.featureextension.- Feature: A high-level description of a software feature.
- Scenario (or Example): A concrete example illustrating a single business rule within the feature.
- Given: Describes the initial context or precondition. It sets the stage for the test.
- When: Describes an action performed by the user or an event that occurs.
- Then: Describes the expected outcome or result. This is the assertion part of the test.
- And, But: Used to chain multiple steps under a
Given,When, orThento improve readability without being repetitive. - Background: Defines a set of
Givensteps that are common to all scenarios in a feature file, reducing duplication. - Scenario Outline: A template for running the same scenario with multiple sets of data, useful for data-driven testing.
- Examples: A table of data that accompanies a
Scenario Outline, where each row represents a run of the scenario.
Example Gherkin File
Feature: User Authentication As a user I want to log in to the application So that I can access my personalized content Scenario: Successful login with valid credentials Given I am on the login page When I enter my username "testuser" and password "password123" And I click the "Login" button Then I should be redirected to the dashboardHow It Works
Tools like Cucumber, SpecFlow, or Behave parse these
.featurefiles. Each line (or step) in a Gherkin scenario is mapped to a corresponding block of code called a "step definition." This code contains the actual logic to interact with the application and verify the outcomes. This separation allows the specifications to remain clean and readable while the underlying implementation can be complex.
Origin
Where it comes from.
Etymology
The name 'Gherkin' is a playful nod to 'Cucumber,' the most popular software tool that uses it. A gherkin is a small variety of cucumber, often pickled. This naming reflects the close relationship between the language (Gherkin) and the tool that processes it (Cucumber).
Historical context
Gherkin's origins are deeply tied to the rise of Behavior-Driven Development (BDD), an agile software development practice that evolved from Test-Driven Development (TDD). In the mid-2000s, developers sought better ways to connect tests to business requirements.
Around 2006, Dan North formalized the concepts of BDD, emphasizing the use of a shared, ubiquitous language to describe system behavior. He proposed a template: "Given a certain context, When an event occurs, Then an outcome should be observed."
In 2007, Aslak Hellesøy, inspired by Dan North's work and Specification by Example, created the tool Cucumber. He needed a simple, structured yet human-readable language to write these behavioral specifications. This language became Gherkin. It provided a formal grammar for the Given-When-Then structure, making it possible for a machine to parse the specifications and execute them as automated tests.
The creation of Gherkin was a pivotal moment for BDD. It provided the practical syntax needed to turn the theory of collaborative specification into a reality, enabling teams to create a single source of truth that served as both requirements and executable tests.
Usage
In context.
Our team writes Gherkin scenarios to define acceptance criteria before starting development.
The product owner reviewed the BDD Syntax in the
.featurefile to ensure it accurately reflected the user story's requirements.To automate the tests, the developer needs to implement step definitions for each line in the Gherkin script.
Using the Cucumber Language helps us create living documentation that is easily understood by both technical and non-technical stakeholders.
FAQ
Common questions.
The Given-When-Then structure provides a clear and consistent format for describing behavior. Given establishes the initial state or context before an action occurs. When describes the specific action or event being tested. Then specifies the expected outcome or result of that action. This structure makes scenarios easy to read, understand, and automate.
Taxonomy
Filed under.
Categories
Tags