Skip to main content

Cucumber & BDD cheatsheet

A one-page reference for Cucumber/BDD. For the full walkthrough and anti-patterns, see the complete guide.

๐Ÿ“– Full guide: Cucumber & BDD โ†’

Gherkin syntaxโ€‹

Feature: Login
Scenario: Valid credentials
Given I am on the login page
When I enter valid credentials
And I click "Sign in"
Then I should see the dashboard

Step definitionsโ€‹

@Given("I am on the login page")
public void onLoginPage() {
driver.get("/login");
}

@When("I enter valid credentials")
public void enterValidCreds() {
loginPage.login("abhishek", "secret123");
}

Data tablesโ€‹

Scenario: Create multiple users
Given the following users exist:
| name | role |
| Alice | admin |
| Bob | user |
@Given("the following users exist:")
public void createUsers(DataTable table) {
List<Map<String,String>> rows = table.asMaps();
}

Scenario Outlineโ€‹

Scenario Outline: Login attempts
When I login as "<user>" with "<pass>"
Then I see "<result>"

Examples:
| user | pass | result |
| admin | right | success |
| admin | wrong | error |

Hooksโ€‹

@Before
public void setup() { driver = new ChromeDriver(); }

@After
public void teardown(Scenario s) {
if (s.isFailed()) captureScreenshot();
driver.quit();
}

@Before("@tag") / @After("@tag") scope hooks to tagged scenarios only.

Tagsโ€‹

@smoke @regression
Scenario: Critical path
mvn test -Dcucumber.filter.tags="@smoke and not @wip"

Common anti-patternsโ€‹

  • Imperative steps ("click button at x,y") instead of declarative ("I log in") โ€” couples feature files to UI details.
  • One giant step definition class instead of composable, reusable steps.
  • Using Gherkin for pure technical/unit tests โ€” BDD's value is business-readable specs, not a syntax tax on every test.
See: Common Anti-Patterns