← Back to libraryQuestion 249 of 468
🥒Cucumber / BDDBeginner

Gherkin Syntax — Feature, Scenario, Given/When/Then

📌 Definition:

Gherkin is the plain-text, keyword-driven language Cucumber reads. Its core keywords are Feature, Scenario, Given, When, Then, And, But, Background, and Scenario Outline/Examples.

📖 Detailed Explanation:

A feature file starts with Feature: (a high-level description), contains one or more Scenario: blocks, and each scenario has steps beginning with Given (context/arrange), When (action/act), Then (outcome/assert), plus And/But to add steps of the same type. Gherkin is indentation-friendly and business-readable. Keywords are just markers — Cucumber matches the TEXT after them to step definitions, so Given/When/Then are interchangeable at the matching level but should be used semantically. Comments start with #, and tags (@smoke) sit above features/scenarios. Writing clear, declarative Gherkin is a skill that determines whether the specs stay readable.

🔑 Key Points:
  • Feature → Scenario → Given/When/Then/And/But steps
  • Given = context, When = action, Then = expected outcome
  • And/But continue the previous step type
  • Keywords are markers; Cucumber matches the step TEXT
🌍 Real-World Example:

Feature: Login\n Scenario: Successful login\n Given the user is on the login page\n When they enter valid credentials\n Then they land on the dashboard — a readable, executable spec.

🎯 Scenario-Based Interview Question:

In Gherkin, what's the semantic role of Given, When, and Then, and does Cucumber technically enforce the difference?