Articles
Published · November 18, 2026

Gherkin Feature Files: Plain-Text Specs With Given, When, Then

A Gherkin feature file is a plain-text spec: one Feature, a few Scenarios, and steps that start with Given, When, or Then. Here is the syntax, with examples.

Also in: Español中文हिन्दी

Quick answer

A Gherkin feature file is a plain-text file with a .feature extension. It starts with one Feature: line, then holds one or more scenarios, and each scenario is a short list of steps that begin with Given, When, or Then. That's the whole shape. Everything else, from Background to Scenario Outline, exists to stop you repeating yourself.

Gherkin is the language Cucumber uses to define test cases), and it is meant to be human readable. You can write one in any text editor. If you like specs that read like RFC 2119 keywords, it'll feel familiar. A small, fixed vocabulary does the structural work.

Step-by-step: anatomy of a .feature file

Start with one Feature

According to the Gherkin reference, the first primary keyword in a document must always be Feature, followed by a colon and a short text. You can only have a single Feature in a .feature file. Tags can sit above it.

Know that it is line oriented

Gherkin is line oriented), a bit like Python: structure comes from whitespace and a few control characters. Spaces or tabs both work for indentation, and the reference recommends two spaces. Every non-blank line starts with a keyword, followed by any text you like. The one exception is free-form description text, covered below.

Here is a small file:

Feature: Shopping list

  Scenario: Add an item
    Given an empty list
    When I add "eggs"
    Then the list contains 1 item

Write steps with Given, When, Then

Each step starts with Given, When, Then, And, or But. The usual reading is: Given sets up the starting state, When is the action, Then is the expected outcome. And and But continue the previous step type so the scenario reads naturally. Gherkin also accepts an asterisk (*) in place of any of them.

Scenario is a synonym of Example, so use whichever you prefer. The reference recommends 3 to 5 steps per example, which keeps each one focused on a single behavior.

One detail catches people out. Keywords are not taken into account when matching a step to its step definition. A Given and a When with identical text are the same step as far as the tooling is concerned, so you can't reuse the text under two keywords.

Share setup with Background

A Background goes before the first scenario, at the same indentation, and its steps run before each one:

Feature: Shopping list

  Background:
    Given I am signed in

  Scenario: Add an item
    When I add "eggs"
    Then the list contains 1 item

You can have only one Background per Feature or Rule, and the reference says to keep it short. Once it grows past a few lines, readers have to carry too much in their heads.

Group scenarios with Rule

Since Gherkin 6, Rule represents one business rule that should be implemented. Put the scenarios that illustrate that rule underneath it:

Feature: Shopping list

  Rule: A list holds each item once

    Example: Adding a duplicate
      Given the list contains "eggs"
      When I add "eggs"
      Then the list still contains 1 item

Repeat a scenario with Scenario Outline

Scenario Outline runs the same scenario with different values. Its steps are a template that is never run directly, and it needs at least one Examples section. Parameters go in angle brackets, and the outline runs once for each row below the header row:

Scenario Outline: Eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |    12 |   5 |    7 |
    |    20 |   5 |   15 |

Add doc strings, data tables, tags, and comments

These are the secondary keywords. A doc string passes a block of text to a step. It is wrapped in three double quotes (backticks also work) on lines of their own, and you can add a content type after the opening delimiter. The indentation of the opening delimiter does not matter, but the indentation inside is significant. If that sounds like YAML multiline strings, it is the same kind of whitespace rule.

Given a file named "notes.md" with:
  """markdown
  # Groceries
  - eggs
  """

A data table uses pipe characters between cells. Inside a cell, write a newline as \n, a pipe as \|, and a backslash as \\. A tag is an @ word placed above a Feature or scenario, used to group and filter.

A comment is a line that starts with #, after zero or more spaces. Comments only work at the start of a new line, and block comments are not supported, which is a notable difference from comment syntax in many languages.

Finally, descriptions. You can write free text under Feature, Example, Background, Scenario Outline, and Rule lines, as long as no description line starts with a keyword. Markdown is allowed there.

Common problems and fixes

  • A step is silently ignored. Some keywords take a colon and some do not. The reference warns that adding a colon to a keyword that should not have one gets your tests ignored. Feature:, Rule:, Example:, Scenario:, Background:, Scenario Outline:, and Examples: take one. Given, When, Then, And, and But do not.
  • A comment breaks the file. Move it to its own line. Trailing comments after a step are not allowed.
  • Two steps clash. Change the wording. Keywords do not count when matching definitions.
  • A line is read as a keyword. If a description line begins with a word like Given, it is parsed as a step. Reword it.
  • Keywords in another language. Add a # language: header on the first line, for example # language: no for Norwegian. Without it the default is English. Gherkin has been translated into more than 70 languages, and some languages have more than one translation for a keyword, per the Gherkin overview.

Doing this with Carets

Feature files are plain text, so they suit Carets well. They stay portable and easy to review in Git.

  • Code and config highlighting. Keywords, comments, and quoted strings are easier to scan when the editor colors them.
  • Files, projects, and tags. Keep a project folder of .feature files next to your notes, and find them again without opening a full IDE.
  • Native on iPhone, iPad, and Mac. Carets is built for Apple platforms, not a web wrapper, so you can fix a typo in a scenario from wherever you are.

Drafting specs before anyone writes code? Carets is a quick place to do it. Get Carets on the App Store for iPhone, iPad, and Mac.

Frequently Asked Questions

What file extension do Gherkin files use?

Gherkin files use the .feature extension, and each file holds a single Feature.

Can I put a comment in the middle of a line?

No. A comment must start on a new line, with zero or more spaces before the #. Block comments are not supported.

Is Scenario the same as Example?

Yes. Scenario is a synonym of Example, so either keyword starts a scenario.

Do Given, When, and Then change how a step is matched?

No. Keywords are not taken into account when looking for a step definition, so two steps with the same text clash even under different keywords.