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.
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
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.
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.
Evidence is attached in Then steps and assertions, not on every action: the report shows what was confirmed, not a flipbook.
How it runs
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).
What was hard
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.