What is Gherkin?

Gherkin makes test scenarios easy to read

With Gherkin, you can use your native language to describe test cases instead of difficult to read and understand camelcase test-method-names.

Feature: Calculator
 
Calculator for adding two numbers
 
@mytag
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered <First> into the calculator
And I have entered <Second> into the calculator
 
When I press add
Then the result should be <Result> on the screen
Examples:
| First | Second | Result |
| 50    | 70     | 120    |
| 30    | 40     | 70     |
| 60    | 30     | 90     |

Learn Gherkin-syntax (Given-When-Then)

It is designed to be a non-technical and human-readable way of describing use cases in software. Gherkin syntax has a few keywords that indicate a special behavior.


Feature

Each feature-file starts with the feature-stopword. It’s unique and provides a high-level description of a software feature to group related scenarios.

Feature: Calculator

Feature description

A human-readable description follows the feature.

You can write anything you like, as long it doesn’t start with a Gherkin-keyword.

Feature: Calculator
 
Calculator for adding two numbers

Scenario

A single Gherkin scenario is a flow of events through the Feature being described and maps 1:1 with an executable test case for the system.

You can have multiple Scenarios in one Feature-file.

Feature: Calculator
 
Calculator for adding two numbers
 
Scenario: Add two numbers

Scenario description

A human-readable description follows the scenario.

You can write anything you like, as long it doesn’t start with a Gherkin-keyword.

Feature: Calculator
 
Calculator for adding two numbers
 
Scenario: Add two numbers
Add two numbers with the calculator

Given

Given steps are used to describe the initial context of the system – the scene of the scenario. It is typically something that happened in the past.

Feature: Calculator
 
Calculator for adding two numbers
 
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered 50 into the calculator

And

And-keyword is used to extend to your steps.

Helps you to elaborate your feature-file further.

Feature: Calculator
 
Calculator for adding two numbers
 
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered 50 into the calculator
And I have entered 70 into the calculator

When

When keyword defines the test action which will be executed.

By test action we mean the user input action.

Feature: Calculator
 
Calculator for adding two numbers
 
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
 
When I press add

Then

Then keyword defines the outcome of previous steps.

Feature: Calculator
 
Calculator for adding two numbers
 
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
 
When I press add
Then the result should be 120 on the screen

Tags

You can place tags above Feature to group related features, independent of your file and directory structure.

Furthermore you can filter tags within your CI/CD-pipeline.

Feature: Calculator
 
Calculator for adding two numbers
 
@mytag
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
 
When I press add
Then the result should be 120 on the screen

Delimited parameters

Delimited parameters <> are used as references which are referred to in the example tables. SpecFlow will automatically replace these parameters when executing tests.

Feature: Calculator
 
Calculator for adding two numbers
 
@mytag
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered <First> into the calculator
And I have entered <Second> into the calculator
 
When I press add
Then the result should be <Result> on the screen

Data tables

Data Tables are handy for passing a list of values to a step definition. A useful tool in complex Gherkin examples.

Feature: Calculator
 
Calculator for adding two numbers
 
@mytag
Scenario: Add two numbers
Add two numbers with the calculator
Given I have entered <First> into the calculator
And I have entered <Second> into the calculator
 
When I press add
Then the result should be <Result> on the screen
Examples:
| First | Second | Result |
| 50 | 70 | 120 |
| 30 | 40 | 70 |
| 60 | 30 | 90 |

Feature files in Living Documentation

Display, review, and discuss your Gherkin feature files and scenarios at any point that makes sense in your CI/CD process. Overlay test results and automatically update your documentation with every new build.

 

More on LivingDoc

BDD & Gherkin

Gherkin is the beating heart of BDD. It helps users formulate requirements in an easy-to-read language.

More on BDD

Ready to write Feature files?

Start using SpecFlow now