Writing Tests in Gherkin (Given-When-Then)
Gherkin structure
Section titled “Gherkin structure”A .feature file contains:
- Feature
- Scenario
- Steps (Given/When/Then)
Example feature
Section titled “Example feature”Feature: Login
As a registered user
I want to login
So that I can access my account
Scenario: Valid login
Given I am on the login page
When I login with username "alice" and password "secret"
Then I should see the dashboardExample step definition
Section titled “Example step definition”from behave import given, when, then
@given("I am on the login page")
def step_open_login(context):
# open browser or call app
pass
@when('I login with username "{username}" and password "{password}"')
def step_login(context, username, password):
pass
@then("I should see the dashboard")
def step_dashboard(context):
pass- Keep steps reusable
- Don’t put too many details in Gherkin
- Use Gherkin for business behavior, not technical design
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading