Allure pytest-bdd integration
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.
Common errors
-
pytest: error: unrecognized arguments: --alluredir=allure-results
cause The `allure-pytest` plugin is not installed or not properly registered with pytest.fixEnsure `allure-pytest-bdd` (which depends on `allure-pytest`) is installed: `pip install allure-pytest-bdd`. -
ModuleNotFoundError: No module named 'pytest_bdd'
cause The `pytest-bdd` library is not installed, which is a core dependency for `allure-pytest-bdd`.fixInstall `pytest-bdd`: `pip install pytest-bdd` (or `pip install allure-pytest-bdd` which includes it). -
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.fixFirst, 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. -
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.fixVerify 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.
- 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).
- 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.
- 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.
Install
-
pip install allure-pytest-bdd -
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