skip to content

In a Cucumber .feature file, which lines does the Gherkin parser treat as keywords, and what happens to a line that matches none?

level: juniorimportance: must knowfreq 80%

answer

  1. A closed vocabulary, not free prose
  2. Free text is legal in exactly one place
  3. Description sits under a keyword line
  4. One Feature keyword per .feature file
  5. Anything else stops the file parsing

basics

~20 s

Gherkin recognises a fixed keyword set - Feature, Rule, Background, Scenario, Scenario Outline, Examples and the Given/When/Then/And/But step keywords. Other text is free-form description where the grammar allows it, and a parse error that fails the whole file anywhere else.

solid answer

~40 s

The Gherkin parser accepts a closed vocabulary: `Feature`, `Rule`, `Background`, `Scenario` (or `Example`), `Scenario Outline`, `Examples`, the step keywords `Given`/`When`/`Then`/`And`/`But`/`*`, plus tag lines starting `@`, whole-line `#` comments, `|` table rows and the `"""` doc-string delimiter. Everything else is free-form **description** text - but only in one position: directly under a `Feature`, `Rule`, `Background`, scenario or `Examples` line, until the next keyword line. Elsewhere it is a syntax error, and Cucumber fails to read the whole file rather than skipping the offending scenario. The structural rules that go with this: one `Feature` per file; at most one `Background` per `Feature` and per `Rule`, positioned before that container's scenarios; and comments must occupy a whole line, since Gherkin has no trailing-comment syntax.

code

gherkin · 12 lines
gherkin
# Menus freeze at the 09:47 cut-off
Feature: School meal ordering
  Parents order meals until the daily cut-off.
  The kitchen headcount is frozen after it.

  Background:
    Given the kitchen can prepare 318 meals

  Scenario: Order lands before the cut-off
    Given the clock reads 09:41
    When a parent orders 2 vegetarian meals
    Then the order is accepted

go deeper

for a junior

Be ready to list the keywords from memory and to say where each may appear. Interviewers open with this because everything else in Cucumber rests on it, and hesitating here reads as never having written a feature file.

for a middle

Explain the mechanics: description text is legal only under a keyword line, one Feature per file, Background before the scenarios of its container, comments whole-line only. Say clearly that an unrecognised line fails the file at parse time rather than skipping a scenario.

for a senior

Show you have debugged this. A parse failure surfaces as a run with no scenario results, which reads like an infrastructure fault; be ready to describe how you spot it fast and how you keep non-engineers editing feature files without breaking them.

for a principal

Own the policy: who may edit feature files, whether the parse runs in CI on every change to the feature directory, and how narrative is kept in description blocks so a shared suite stays readable to the domain experts it is written for.

## The lines Gherkin treats as keywords A `.feature` file is not free prose with some keywords sprinkled in. Cucumber parses it with the Gherkin parser, which recognises a **closed vocabulary** and gives every other line one of exactly two fates. In the English dialect the recognised line starts are: | Line begins with | Gherkin calls it | Where it is legal | |---|---|---| | `Feature:` | the feature header | once per file, at the top | | `Rule:` | a business-rule grouping | between `Feature` and its scenarios | | `Background:` | shared setup steps | once per `Feature`, once per `Rule`, before that container's scenarios | | `Scenario:` / `Example:` | one concrete example | under `Feature` or under `Rule` | | `Scenario Outline:` / `Scenario Template:` | a parameterised scenario | same places as `Scenario` | | `Examples:` / `Scenarios:` | the rows of an outline | under the outline it belongs to | | `Given` `When` `Then` `And` `But` `*` | step keywords | inside `Background`, `Scenario` or an outline | | `@` | a tag line | on its own line above `Feature`, `Rule`, a scenario or `Examples` | | `#` | a whole-line comment | anywhere | | `\|` | a table row | as a step argument or inside `Examples` | | `"""` or triple backticks | a doc-string delimiter | directly under a step | `Scenario` and `Example` are synonyms, as are `Examples` and `Scenarios`, and `Scenario Outline` and `Scenario Template`. The step keyword is **not** part of what a step definition matches — it is stripped before matching — which is why `Given`, `And` and `*` are interchangeable to the glue layer and matter only to the human reader. ## The structural rules the parser enforces 1. **One `Feature` per file.** A second `Feature:` line is a parse error, not a second feature. If a merge leaves two of them in one file, split the file. 2. **`Background` is positional.** At most one per container, and it must come before the first scenario of that container. A `Background` written after a scenario fails to parse. 3. **Steps need a home.** A `Given`/`When`/`Then` line before any `Background` or scenario has nothing to attach to and fails. 4. **Comments are whole-line only.** The `#` must be the first non-whitespace character on the line. Gherkin has no trailing-comment syntax, so `When the cut-off passes # 09:47` is a step whose text ends in `# 09:47`, and your step definition now has to match that. 5. **Tags live on their own line** above the element they decorate; a tag written after text on a keyword line is not a tag. ## What happens to a line that matches nothing Gherkin allows **free-form description text** in one position: immediately after a `Feature:`, `Rule:`, `Background:`, scenario or `Examples:` line, running until the next keyword line. Those lines are kept as that element's description and are never executed. This is where the narrative belongs — the paragraph explaining who the feature is for, or a link to the rule it implements. Anywhere else, an unrecognised line is a **parse error**. The parser reports the file, the line and the kinds of token it expected there, and that file does not run. The failure is a *reading* failure, so it happens before any scenario starts: you do not get "one bad scenario, the rest pass". That is the single most useful thing to know about this rule, because the instinct — carried over from languages where an unknown line is a comment or a no-op — is that Cucumber will simply skip it. ## Why it bites on a shared suite Consider a school-meal ordering service whose feature files are edited by both the platform team and the kitchen-operations team. Someone pastes a two-line note between the `When` and the `Then` of a scenario to explain a cut-off time. Locally nothing looks wrong; the file is still readable English. On CI the whole file stops parsing, and because the run fails at discovery the report shows no scenario results at all — which reads like an infrastructure problem rather than a syntax one. The fix is a one-character edit: put a `#` in front of the note, or move it into the description block under `Scenario:`. Two habits keep this cheap: - **Parse in CI on every change** to the feature directory, so a malformed file is caught by the build rather than by whoever runs the suite next. - **Teach the description block.** Non-engineers editing feature files want to write prose; give them the one legal place for it instead of relying on them to remember which lines are keywords. ## Checking yourself The compact way to hold this: Gherkin is closer to a **form with named fields** than to prose. Each keyword opens a field, the parser knows which fields may follow which, and the only free text it accepts is the description blank each field leaves for you. Everything else is either a step, a table row, a tag, a comment, a doc-string delimiter — or an error that stops the file.

  • Where may a comment go in a Gherkin feature file, and can you put one at the end of a step line?
    Comments are whole-line only: the `#` must be the first non-whitespace character. Gherkin has no trailing-comment syntax, so `When the cut-off passes # 09:47` is a step whose text ends in `# 09:47`, and the step definition would have to match that text. Put the note on its own `#` line, or in the description block.
  • A bad merge leaves two Feature lines in one file. What does Cucumber do?
    It fails to parse the file. The Gherkin grammar allows at most one `Feature` per file, so the second `Feature:` line is a syntax error rather than a second feature. Nothing in the file runs. The fix is to split the content into two `.feature` files.
  • A product owner wants two paragraphs of narrative in a feature file. Where do they go?
    In the description block: free-form lines directly under the `Feature:`, `Rule:` or scenario line, ending at the next keyword line. They are kept as that element's description and never executed. Whole-line `#` comments are the other option. What breaks is prose placed between steps, which is a parse error.

A feature file is closer to a form with named fields than to an essay: each keyword opens a field, and the only free text the parser accepts is the description blank each field leaves for you.

saying these in an interview costs you the question

  • Thinks Gherkin silently ignores any line it cannot parse
  • Believes several Feature keywords can share one file
  • Puts a trailing # comment on the end of a step line
  • Thinks a syntax error only skips the offending scenario
  • Treats a feature file as prose with keywords sprinkled in