Allure pytest-bdd integration
raw JSON → 2.15.3 verified Fri Apr 17 auth: no python
allure-pytest-bdd provides integration between the Allure reporting framework and pytest-bdd, allowing users to generate rich, interactive test reports for BDD scenarios written with Gherkin. It is part of the broader Allure Python ecosystem, currently at version 2.15.3, and receives frequent, usually minor, updates.
pip install allure-pytest-bdd Common errors
error pytest: error: unrecognized arguments: --alluredir=allure-results ↓
cause The `allure-pytest` plugin is not installed or not properly registered with pytest.
fix
Ensure
allure-pytest-bdd (which depends on allure-pytest) is installed: pip install allure-pytest-bdd. error ModuleNotFoundError: No module named 'pytest_bdd' ↓
cause The `pytest-bdd` library is not installed, which is a core dependency for `allure-pytest-bdd`.
fix
Install
pytest-bdd: pip install pytest-bdd (or pip install allure-pytest-bdd which includes it). error No results found. Wiping out allure-results folder ↓
cause The `allure serve` command was run, but the specified results directory (`allure-results` by default) either doesn't exist or contains no valid Allure report data.
fix
First, run your tests with
pytest --alluredir=allure-results to generate the report data. Then, ensure you are running allure serve from the same directory where allure-results was created, or specify the correct path. error pytest_bdd.exceptions.NoScenarioFound: No scenario 'Scenario: User logs in successfully' in features/example.feature ↓
cause The scenario name or feature file path in the `@scenario` decorator does not exactly match the Gherkin feature file.
fix
Verify that the scenario name and feature file path provided in
@scenario('features/example.feature', 'Scenario: User logs in successfully') precisely match the content and location of your .feature file. Warnings
gotcha Viewing Allure reports requires the separate Allure commandline tool to be installed and run after tests. Generating test results with `--alluredir` only produces raw data files. ↓
fix Install `allure-commandline` (e.g., `npm install -g allure-commandline`) and run `allure serve allure-results` after `pytest --alluredir=allure-results`.
breaking Prior to version 2.14.0, the `allure-pytest-bdd` integration had limited support for the full Allure API (e.g., dynamic titles, attachments within steps, xfail). ↓
fix Upgrade to `allure-pytest-bdd>=2.14.0` to fully leverage the Allure API and ensure all features are supported, including expected failures (xfail), dynamic titles, gherkin tags conversion, and descriptions from feature/scenario metadata.
gotcha In `allure-pytest-bdd` versions before 2.14.3, built-in pytest markers might have been incorrectly converted to Allure tags, leading to unintended tags in reports. ↓
fix Upgrade to `allure-pytest-bdd>=2.14.3` to ensure that only user-defined markers and Gherkin tags are converted to Allure tags, avoiding conflicts with reserved pytest markers.
gotcha When using pytest version 8.4 or later with `allure-pytest-bdd` prior to 2.14.3, attempting to change a fixture's title could result in errors. ↓
fix Update `allure-pytest-bdd` to version `2.14.3` or newer to resolve issues with fixture titling in newer `pytest` versions.
Install
npm install -g allure-commandline --save-dev Imports
- allure
import allure - scenario
from pytest_bdd import scenario
Quickstart
# features/example.feature
# Feature: Basic BDD scenario
#
# Scenario: User logs in successfully
# Given a user is on the login page
# When the user enters valid credentials
# Then the user should be redirected to the dashboard
# test_example.py
import pytest
import allure
from pytest_bdd import scenario, given, when, then
@scenario('features/example.feature', 'User logs in successfully')
def test_login():
"""This is the test for the login scenario."""
pass
@given('a user is on the login page')
def login_page():
allure.step("Navigated to login page")
assert True
@when('the user enters valid credentials')
def enter_credentials():
with allure.step("Entering username and password"):
allure.attach("username: test_user", name="credentials", attachment_type=allure.attachment_type.TEXT)
assert True
@then('the user should be redirected to the dashboard')
def dashboard_redirect():
allure.dynamic.tag("login-flow")
assert True
# To run tests and generate Allure report:
# pytest --alluredir=allure-results
# To open the report in your browser (requires Allure commandline):
# allure serve allure-results