Beginner
Chapter 3 · 10 min read
Gherkin Syntax Deep Dive
Master Gherkin keywords — Feature, Scenario, Given/When/Then, And/But, Background, comments, descriptions, and writing good scenarios.
Gherkin Syntax in Detail
Gherkin is a structured, human-readable language for describing software behavior. Every line starts with a keyword that tells Cucumber what the line does.
Core Keywords
- Feature: — Describes a high-level feature with a title and optional description
- Scenario: — A concrete example of how the feature should behave
- Given — Sets up preconditions (state before the action)
- When — Describes the action the user performs
- Then — States the expected outcome
- And / But — Continue the previous Given, When, or Then
- Background: — Steps that run before every scenario in a feature
Writing Good Scenarios
Good Gherkin scenarios are declarative (what, not how), use business language (not technical details), and each scenario tests exactly one behavior. Avoid coupling scenarios to implementation details.
search.feature
Feature: Product Search
As a customer
I want to search for products
So that I can find what I need quickly
# Background runs before EVERY scenario in this feature
Background:
Given the user is on the home page
And the user is logged in as "customer"
Scenario: Search by product name
When the user searches for "laptop"
Then search results should be displayed
And the results count should be greater than 0
But the results should not contain "television"
Scenario: Search with no results
When the user searches for "xyznonexistent123"
Then the "No results found" message should be displayed
And the results count should be 0
Scenario: Search with special characters
When the user searches for "laptop & mouse"
Then search results should be displayed
# This scenario is tagged for selective execution
@smoke @priority-high
Scenario: Empty search
When the user searches for ""
Then all products should be displayed
search.feature
Feature: Product Search
As a customer
I want to search for products
So that I can find what I need quickly
# Background runs before EVERY scenario in this feature
Background:
Given the user is on the home page
And the user is logged in as "customer"
Scenario: Search by product name
When the user searches for "laptop"
Then search results should be displayed
And the results count should be greater than 0
But the results should not contain "television"
Scenario: Search with no results
When the user searches for "xyznonexistent123"
Then the "No results found" message should be displayed
And the results count should be 0
Scenario: Search with special characters
When the user searches for "laptop & mouse"
Then search results should be displayed
@smoke @priority-high
Scenario: Empty search
When the user searches for ""
Then all products should be displayed
Cucumber BDD
Beginner
Gherkin Syntax Deep Dive
Written by PV
© 2026 All Rights Reserved