What Is BDD and Gherkin

BDD in One Paragraph

Behaviour-Driven Development (BDD) means the business analyst, developer and tester agree on examples of behaviour before coding, written in plain English. Cucumber runs those examples as automated tests. The language of the examples is called Gherkin.

KeywordMeaning
Featurethe feature being described (one per file)
Scenarioone example / test case
Givenstarting state (precondition)
Whenthe action the user takes
Thenthe expected result
And / Butmore steps of the same kind
Backgroundsteps shared by every scenario in the file

A Feature File

Example

src/test/resources/features/login.feature

Feature: Login
  As a ShopEase customer
  I want to log in
  So that I can see my orders

  Background:
    Given the user is on the login page

  @smoke
  Scenario: Valid login
    When the user logs in with "[email protected]" and "Secret@123"
    Then the welcome message "Welcome, Asha" is shown

  Scenario: Wrong password
    When the user logs in with "[email protected]" and "wrong"
    Then the error "Invalid email or password" is shown

Dependencies

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.20.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>7.20.1</version>
    <scope>test</scope>
</dependency>

Note: BDD is a collaboration practice. If only testers read the feature files, Cucumber adds a layer without the benefit; plain TestNG + POM may be simpler.