-
Notifications
You must be signed in to change notification settings - Fork 252
feat: Allure API implementation for Pytest-BDD #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Mar 24, 2025
Closed
baev
approved these changes
Mar 26, 2025
This was referenced Mar 26, 2025
Closed
Closed
Closed
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
The PR implements the missing parts of Allure API in Pytest-BDD. Check #726 for the complete list of the implemented API pieces.
API usage notes
To use Allure API, apply a decorator or call a runtime function (
allure.dynamic.*
,allure.attach
, orallure.attach.file
).Allure decorators
General usage: apply decorators to a scenario test function:
Warning
It is important to put Allure decorators above
@scenario
. In other words, the following won't work:Apply to whole modules
All Allure decorators except
@allure.title
and@allure.step
are also proper pytest markers, which means they can be applied to the whole module via thepytestmark
global variable.Note
Although pytest markers can also be defined at the class level, Pytest-BDD doesn't support defining scenario test functions inside a class (see pytest-dev/pytest-bdd#325).
The
scenarios
shortcutAllure decorators can't be applied directly to test functions defined with the
scenarios
shortcut. But you have some options to tackle that.Option 1 is to define separate test functions with the
@scenario
decorator and apply Allure decorators to them. Make sure all such functions are defined beforescenarios
is called:This works well for scenario-specific information like IDs, titles, descriptions, issue links, etc.
Option 2 is to use
pytestmark
as in one of the above examples:This is preferable for information that applies to many scenarios, like epics, general links, suites, tags, etc.
Option 3: call the runtime counterparts from a step function:
Note
When possible, prefer Allure decorators over runtime functions. The decorators will affect the test results regardless of the outcome. On the other hand, the runtime function calls may be skipped because of failures in fixtures or preceding steps.
About
@allure.title
When applied to a scenario function,
@allure.title
supports interpolation of outline arguments and pytest parameters:The test result will be displayed as
Outline arg: gherkin, pytest arg: pytest
. You can read more about the syntax of replacement fields here.Note
Fixture values currently can't be interpolated into scenario names.
When applied to a step implementation,
@allure.title
will update the step's name. The following stuff can be interpolated:Example:
Here, the title will be
Outline arg: gherkin, pytest arg: pytest, fixture value: fixture, prev step result: step-result, step arg: 2
This implements #737.
Runtime functions
The Allure Runtime API functions can be called anytime between
pytest_bdd_before_scenario
and the last call topytest_runtest_makereport
for the current item. That includes step implementations, scenario test functions, and some pytest hooks:In conftest.py:
allure.dynamic.title
Unlike its decorator counterpart,
allure.dynamic.title
always changes the test result's name. It never affects a step's name.As in
allure-pytest
, no interpolation is supported: the caller must fully construct a value.allure.attach
andallure.attach.file
These functions add attachments to the innermost scope. If a step is running, the attachment will go to the current step. If no step is running, the attachment will be added to the test result.
allure.dynamic.parameter
This function always adds a new parameter to the test result. It neither changes the value or metadata of an existing parameter nor adds parameters to steps.
The
mode
andexcluded
arguments work just like in pytest.allure.step
Substeps can now be added with
allure.step
. It works just like in pytest.Other changes
Data tables and doc strings
Pytest-BDD 8.0 features data tables and doc strings. This PR supports both arguments by converting them to the corresponding attachments. This implements #844.
This also works for older versions of Pytest-BDD. In the case of data tables, you should name the argument
datatable
and define a type converter. The converter must return the data table aslist[list[str]]
:For doc strings, you should only name the argument
docstring
. The type converter (if used) must returnstr
:Feature and scenario descriptions
Feature and scenario descriptions from feature files are now shown as test result descriptions (separated by an empty line).
@allure.description
andallure.dynamic.description
overwrite these descriptions.Step arguments
Arguments of a step implementation function are now shown as the step's parameters in the report.
Xfail support
Allure Pytest-BDD now correctly interprets expected failures:
@pytest.mark.xfail
markers and calls topytest.xfail
).Broken status support
Scenarios/steps with unexpected errors are now reported as broken (with yellow). Scenarios/steps with failed assertions are still reported as failed (with red).
Link templates
Link templates are now supported via the
--allure-link-pattern
CLI option. The format is the same as forallure-pytest
. You may put this option in your pytest configuration to automatically add it on each run. For example, in pyproject.toml:Note
If a value passed to an Allure link API is already a proper URL, no template is used. The value will be reported as is.
Pytest and gherkin tags
Argument-less pytest markers (except built-in markers) are now converted to Allure tags. Since Pytest-BDD implements gherkin tags as custom pytest markers, those are converted to tags as well.
Given the following feature file:
And the following Python file:
The test result will contain three tags:
foo
,bar
, andbaz
.Minor changes and fixes
passed
)Topic :: Software Development :: Testing :: BDD
classifier toallure-pytest-bdd
.Fixes #655
Closes #726
Closes #737
Closes #844
Checklist