portfolio / projects / bdd
andresqe.duckdns.org · HTTPS · up
Case study · BDD ● CI passing

BDD with pytest-bdd: readable scenarios that reuse the same Page Objects

Two Gherkin scenarios (a visitor booking, and an admin adding a room) whose steps call the same classes as the plain tests: one source of truth for two audiences.

Role
Sole author
Stack
pytest-bdd · Playwright · allure-pytest-bdd
Scenarios
2, written in Spanish

The problem

Badly done BDD ends up as a second framework: duplicated steps, repeated selectors and scenarios nobody reads. It is double maintenance without the payoff of business people understanding the tests.

The approach

.feature files describe behavior in business language and the step definitions are thin layers calling the Page Objects that already exist. pytest-bdd generates one test per scenario, so they run with the same pytest, the same fixtures and the same CI.

Key decisions

01 The .feature files live next to the tests

They are inside ui-tests/booking-flow and not at the repo root: that way the steps import the Page Objects without tampering with sys.path.

02 Steps really validate

After login the test waits for the new thing (the logout button) and for the old thing (the form) and the loading indicator to go away before capturing evidence: in a SPA, trusting that something appeared gives half-loaded screenshots.

03 Screenshots only where it validates

Evidence is attached in Then steps and assertions, not on every action: the report shows what was confirmed, not a flipbook.

How it runs

Gherkin (.feature)→ step definitions→ Page Objects→ Playwright→ Allure with Given/When/Then
Feature: Gestión de habitaciones por el administrador
  Como administrador del hotel
  Quiero crear nuevas habitaciones
  Para mantener actualizado el inventario disponible

  Scenario: Crear una habitación con servicios
    Given que inicio sesión como administrador
    When que creo una habitación nueva con tipo, precio y servicios
    Then que la habitación aparece en el listado de habitaciones correctamente

Real feature from ui-tests/booking-flow/features/admin_room.feature (in Spanish).

BDD scenarios2out of the 15 cases in the UI suite
Browsers3both run in the CI matrix
Block the build1 of 2the admin one does; the booking one does not

What was hard

The report with Given/When/Then

allure-pytest-bdd gives the per-step breakdown, but it cannot be active together with allure-pytest, and on its own it does not work for non-BDD tests. That is why they run with separate commands and separate result folders.

Limits

  • There is one scenario per feature: it covers the main flow, not negative cases (those live in plain tests, for example the invalid phone or the wrong login).
  • The booking scenario shares the booking flow's fragility with the practice site, which is why it does not block the build in CI.