BDD — Explained with Examples
BDD (Behavior-Driven Development) extends TDD by writing tests in plain language using Given/When/Then scenarios understandable by non-technical stakeholders.
BDD stands for Behavior-Driven Development, introduced by Dan North in 2003. It bridges the communication gap between developers, testers, and business stakeholders by using a shared, readable language for defining application behavior.
Gherkin Language
BDD uses Gherkin syntax, which structures scenarios in three parts:
Feature: User Login
As a registered user
I want to log into the application
So that I can access my account
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter "alice@example.com" in the email field
And I enter "CorrectPass123" in the password field
And I click the "Login" button
Then I should be redirected to the dashboard
And I should see "Welcome, Alice"
Scenario: Failed login with invalid password
Given I am on the login page
When I enter "alice@example.com" in the email field
And I enter "WrongPass" in the password field
And I click the "Login" button
Then I should see "Invalid email or password"
And I should remain on the login pageExample: Cucumber.js Implementation
// features/login.feature (Gherkin — shared with stakeholders)
// (shown above)
// features/step_definitions/login_steps.js (code)
const { Given, When, Then } = require('@cucumber/cucumber');
const { expect } = require('chai');
const { LoginPage } = require('../pages/login');
Given('I am on the login page', async function () {
this.page = new LoginPage();
await this.page.navigate();
});
When('I enter {string} in the email field', async function (email) {
await this.page.enterEmail(email);
});
When('I click the {string} button', async function (button) {
await this.page.clickButton(button);
});
Then('I should be redirected to the dashboard', async function () {
const url = await this.page.getCurrentUrl();
expect(url).to.include('/dashboard');
});
Then('I should see {string}', async function (message) {
const text = await this.page.getPageText();
expect(text).to.include(message);
});Real-World Analogy
BDD is like writing a recipe in a cookbook. The recipe says: “Given you have flour, eggs, and sugar (setup), when you mix them and bake at 350°F (action), then you get a cake (outcome).” Both a professional chef and a home cook understand this. The recipe doesn’t say “instantiate Oven object with temperature 350” — it’s in plain language that anyone can read and verify.
BDD vs TDD
| Aspect | TDD | BDD |
|---|---|---|
| Focus | Code correctness | Business behavior |
| Language | Programming language | Natural language (Gherkin) |
| Audience | Developers | Developers + Business |
| Output | Unit tests | Automated acceptance tests |
| Scope | Function/class level | Feature/user story level |
Related Terms
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro