liveBook · Manning

Gherkin

As explained in Effective Behavior-Driven Development

A domain-specific language for writing human-readable behavior specifications using constructs such as Given, When, and Then.

Gherkin turns shared understanding of desired behavior into business-readable specifications. Its simple, structured syntax can also be processed by automation tools; using it for test scripts alone does not mean a team is practicing behavior-driven development. section 6.6 section 8.2

A feature file is plain text, usually ending in .feature, organized with a small set of keywords:

  • Feature names the feature; Rule groups scenarios that illustrate a business rule; Scenario introduces an example. section 6.3
  • Given, When, and Then introduce scenario steps. And joins steps; automation frameworks can use step text to identify code to run. section 6.3
Listing 6.1 Sample feature file (with only one scenario fully fleshed out)
Feature: Change delivery address

  Rule: Allow address change if not picked up yet


    Scenario: Order waiting for pickup
      Given the customer has placed an order
      And the order is waiting for pickup
      When they request to change the delivery address
      Then the address change should be allowed

    Scenario: Request to change address received after order collection 
      Given …
      When …
      Then …
      
    Scenario: Order picked up during address change process
      Given …
      When …
      Then …
  
  Rule: New address should be in range

    Scenario: New address is in range
      Given …
      When …
      Then …

    Scenario: New address is out of range
      Given …
      When …
      Then …

The delivery-address feature shows how rules group examples, including whether an order can be changed before pickup and whether a new address is in range. section 6.3

Use business language that stakeholders and delivery teams can understand. Gherkin is tool-independent, supports multiple spoken languages, and is compatible with scenario automation tools. Automatically checked specifications can serve as living documentation, alerting teams when documented behavior and implementation diverge. section 6.6 section 6.3

For legacy systems, avoid copying manual test scripts directly into Gherkin. Assess which scenarios will provide valuable documentation, especially around functionality likely to change. section 11.4

How Effective Behavior-Driven Development covers it

How deep it goes
Defines feature, rule, scenario, and step keywords, but leaves automation implementation to compatible tools.
Examples
Uses delivery-address change scenarios organized around pickup status and address range.
What it assumes
Requires no programming knowledge and relies on understanding the business behavior being specified.
What you can do after
Build from scratch by writing business-readable scenarios with Given, When, and Then steps.
How it connects
Connects to behavior-driven development, living documentation, and scenario automation.

Also explained in

Compare how 2 books explain Gherkin →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.