๐ฅ BDD & Cucumber
BDD framework philosophy, Gherkin syntax (Given/When/Then), feature files, and step definitions.
What is Behavior-Driven Development (BDD)?
BDD bridges the communication gap between business analysts, developers, and QA engineers by writing software specs in plain human-readable language.
Gherkin Syntax Keywords
Gherkin is the structured English syntax used in Cucumber feature files.
| Keyword | Purpose | Example |
|---|---|---|
| Feature | Title & summary of functionality | Feature: User Authentication |
| Scenario | Specific test case or user flow | Scenario: Successful login with valid email |
| Given | Precondition state of system | Given user is on login page |
| When | Action executed by user | When user enters valid credentials |
| Then | Expected outcome assertion | Then user should see dashboard |
Cucumber Feature File Example
Writing plain text scenarios in a `.feature` file.
login.feature
Gherkin
Feature: User Login Feature
Scenario Outline: Login with multiple user credentials
Given User is on login page
When User enters "" and ""
And Clicks submit button
Then Login status should be ""
Examples:
| username | password | status |
| admin | pass123 | success |
| guest | wrong | fail |
Step Definition Class in Java
Mapping Gherkin steps to Java execution code using `@Given`, `@When`, `@Then` annotations.
LoginSteps.java
Java
import io.cucumber.java.en.*; public class LoginSteps { @Given("User is on login page") public void openLoginPage() { System.out.println("Opening login page..."); } @When("User enters {string} and {string}") public void enterCredentials(String user, String pass) { System.out.println("Entering: " + user); } @Then("Login status should be {string}") public void verifyStatus(String status) { System.out.println("Status verified: " + status); } }