Software Testing - Cucumber and BDD Testing
1. What is BDD (Behavior-Driven Development)?
Behavior-Driven Development (BDD) is a software development approach that focuses on how the application should behave from the user’s perspective.
In BDD:
-
Requirements are written in plain English
-
Technical + non-technical team members collaborate
-
Behavior is defined before development begins
BDD helps bridge the communication gap between:
-
Developers
-
Testers
-
Business analysts
-
Product owners
2. Why BDD is Needed
Traditional requirements often lead to misunderstandings.
BDD solves this by:
-
Writing requirements in a human-friendly format
-
Ensuring all team members understand the behavior
-
Reducing missed scenarios
-
Improving communication
-
Enabling automated tests with clear documentation
3. What is Cucumber?
Cucumber is a testing tool that supports BDD.
It allows writing test scenarios in a readable format called Gherkin, which looks like English.
Cucumber lets you:
-
Write test scenarios
-
Map scenarios to automation code
-
Generate readable test reports
Cucumber supports multiple languages like:
-
Java
-
JavaScript
-
Python
-
Ruby
Most commonly used with Selenium + Java.
4. Cucumber Uses Gherkin Syntax
Gherkin is a structured language used to define test scenarios.
It uses keywords like:
-
Feature
-
Scenario
-
Given (precondition)
-
When (action)
-
Then (expected result)
-
And
-
But
5. Example of a Gherkin Feature File
Login Feature (Gherkin Syntax)
Feature: Login functionality
Scenario: Valid login
Given the user is on the login page
When the user enters valid username and password
And clicks on the login button
Then the user should be redirected to the dashboard
This scenario defines behavior, not code.
6. Step Definition (Cucumber + Java Example)
The Gherkin steps are connected to Java methods.
@Given("the user is on the login page")
public void userOnLoginPage() {
driver.get("https://example.com/login");
}
@When("the user enters valid username and password")
public void enterCredentials() {
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("12345");
}
@When("clicks on the login button")
public void clickLoginButton() {
driver.findElement(By.id("loginBtn")).click();
}
@Then("the user should be redirected to the dashboard")
public void verifyDashboard() {
assertEquals("Dashboard", driver.getTitle());
}
7. Cucumber Project Structure
A typical Cucumber + Selenium + Java project contains:
-
Feature files (Gherkin scenarios)
-
Step Definitions (Java code)
-
Runner Class (JUnit/TestNG runner)
-
Page Objects (if using POM)
-
Configuration files
8. Runner Class Example (JUnit)
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = "stepdefinitions",
plugin = {"pretty", "html:target/cucumber-report"}
)
public class TestRunner {
}
9. Advantages of Cucumber & BDD
✔ Easy to Understand
Uses plain English — business users can read it.
✔ Improves Collaboration
POs, developers, and testers work together.
✔ Automatic Documentation
Feature files = living documentation.
✔ Improves Test Coverage
Scenarios are discussed before development.
✔ Works with Selenium
Most teams use Cucumber for UI automation.
✔ Reusable Steps
Steps can be reused across multiple scenarios.
10. When to Use Cucumber?
Use Cucumber when:
-
Requirements need clarity
-
Multiple stakeholders are involved
-
You want readable test cases
-
Behavior is more important than implementation
-
You need both documentation + automation
Do not use Cucumber:
-
For very technical backend testing
-
When the team is not interested in BDD approach
-
If scenarios become too detailed or complex
11. BDD vs TDD
| TDD | BDD |
|---|---|
| Focuses on unit tests | Focuses on behavior |
| Tests are written by developers | Written collaboratively |
| Uses code-level tests | Uses plain English scenarios |
| Tools: JUnit, TestNG | Tools: Cucumber, SpecFlow |
12. Best Practices
-
Keep scenarios short and readable
-
Use the Given–When–Then structure properly
-
Avoid technical details in scenarios
-
Use Page Object Model for step definitions
-
Use meaningful feature names
-
Avoid duplication of steps
-
Review feature files with business teams
-
Automate only stable scenarios