Pytest-bdd is a pytest plugin that implements a subset of the Gherkin language to enable automating project requirements testing and facilitate Behavior-Driven Development (BDD). It integrates seamlessly with pytest, allowing reuse of fixtures and plugins, unifying unit and functional tests, and simplifying continuous integration server configuration. The library is actively maintained, with version 8.1.0 currently available, and often releases updates to ensure compatibility with the latest Gherkin specification.
pip install pytest-bddVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic pytest-bdd setup. It involves creating a Gherkin `.feature` file to define a scenario and a corresponding Python test file (`test_example.py`) where the scenario is linked using `@scenario` and step definitions (`@given`, `@when`, `@then`) are implemented. Pytest fixtures are used for sharing state between steps.
Explicitly define fixtures or use the `target_fixture` parameter in step decorators if you need to expose a step's return value as a fixture. For example, `@given(parsers.parse('I have a {fruit}'), target_fixture='my_fruit')`.Migrate feature-level and vertical examples into `Scenario Outline` blocks using `Examples` tables. Ensure all variable templates `<variable>` are used within `Scenario Outline` steps only.
Ensure that steps containing variable templates like `<variable>` are exclusively used within `Scenario Outline` scenarios. For regular `Scenario` steps that need parameters, use `pytest_bdd.parsers.parse` or other explicit parsers.
Configure the `bdd_features_base_dir` key in your `pytest.ini` (or `tox.ini`, `setup.cfg`) to specify the base directory for feature files, relative to the pytest root directory. Alternatively, provide `features_base_dir` directly to the `@scenario` decorator for per-scenario overrides.
Add all Gherkin tags to the `markers` section in `pytest.ini`. E.g., `[pytest] markers = my_tag: description of my_tag`. Ensure tags use alphanumeric characters and underscores, starting with a non-number.
Update `.feature` files to comply with the official Gherkin specification. Ensure multiline steps are enclosed in triple quotes, every feature file begins with `Feature:`, and tags like `@tag one` are changed to `@tag_one` or separate tags.
Define a Python function with the appropriate `pytest_bdd` decorator (`@given`, `@when`, or `@then`) matching the exact text of the Gherkin step.
```python
from pytest_bdd import given
@given('I have a clean database')
def clean_database():
# Implement database setup here
pass
```Correct the syntax in the `.feature` file to adhere to the Gherkin specification, ensuring correct keywords, indentation, or table formatting.
```gherkin
Feature: User login
Scenario: Successful login
Given I am on the login page
When I enter valid credentials
Then I should be logged in
```Ensure the referenced fixture is correctly defined using `@pytest.fixture` and is available in a scope accessible to the test (e.g., in the test file itself or in a `conftest.py` file in a parent directory).
```python
# In conftest.py or your test file
import pytest
@pytest.fixture
def my_fixture():
return "some_value"
# In your steps file, use the fixture as an argument
from pytest_bdd import given
@given('I use a fixture')
def use_the_fixture(my_fixture):
print(f"Using: {my_fixture}")
```Install the `pytest-bdd` package using pip: `pip install pytest-bdd`