How to Write BDD Tests in Cucumber
✅ What is BDD and Cucumber?
πΉ BDD (Behavior-Driven Development)
A software development approach that encourages collaboration between developers, testers, and business stakeholders by writing test scenarios based on system behavior.
πΉ Cucumber
A testing tool that supports BDD. It lets you write test scenarios in Gherkin syntax, a plain-English format.
π ️ Tools You Need
Java (JDK)
Maven or Gradle
Cucumber (Core, JUnit, Java)
IDE (like IntelliJ or Eclipse)
Optional: Selenium (for UI testing)
π️ Typical Project Structure
bash
Copy
Edit
src
├── test
│ ├── java
│ │ ├── stepDefinitions
│ │ └── testRunner
│ └── resources
│ └── features
✍️ Step 1: Create a Feature File
Location: src/test/resources/features/login.feature
gherkin
Copy
Edit
Feature: Login functionality
Scenario: Successful login with valid credentials
Given User is on the login page
When User enters valid username and password
Then User should be redirected to the dashboard
π§Ύ Step 2: Create Step Definitions
Location: src/test/java/stepDefinitions/LoginSteps.java
java
Copy
Edit
package stepDefinitions;
import io.cucumber.java.en.*;
public class LoginSteps {
@Given("User is on the login page")
public void user_is_on_login_page() {
System.out.println("User navigates to login page");
// Add Selenium code to open browser and navigate
}
@When("User enters valid username and password")
public void user_enters_valid_credentials() {
System.out.println("User enters valid login details");
// Selenium code to enter credentials and submit
}
@Then("User should be redirected to the dashboard")
public void user_redirected_to_dashboard() {
System.out.println("User lands on dashboard page");
// Validate the user is on dashboard
}
}
π§ͺ Step 3: Create Test Runner
Location: src/test/java/testRunner/TestRunner.java
java
Copy
Edit
package testRunner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = {"stepDefinitions"},
plugin = {"pretty", "html:target/cucumber-report.html"},
monochrome = true
)
public class TestRunner {
}
✅ Step 4: Run the Test
Run TestRunner.java as a JUnit test. You should see console output or browser automation (if using Selenium), and a test report.
π§ Tips for Writing Good BDD Scenarios
Use the "Given–When–Then" format consistently.
Keep steps simple, clear, and business-focused.
Avoid technical details in the feature file.
Keep step definitions reusable.
Name feature files and scenarios meaningfully.
π Sample Real-World Scenario (E-commerce)
gherkin
Copy
Edit
Feature: Product search
Scenario: Search for a valid product
Given The user is on the home page
When The user searches for "iPhone"
Then Search results should display items related to "iPhone"
π Summary
Component Purpose
Feature File Contains the test scenarios in Gherkin
Step Definitions Links Gherkin steps to Java methods
Test Runner Runs the tests using JUnit or TestNG
Plugins For reporting (HTML, JSON, etc.)
Learn Testing Tools Training in Hyderabad
Read More
Mobile Automation with Appium 101
Test Automation Basics with Katalon
Open Source vs Paid Testing Tools
Load Testing with JMeter in 10 Minutes
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment