---
title: Pytest
description: Learn how to integrate Allure with pytest to generate rich, interactive test reports. Follow step-by-step setup, test execution, and report generation guidance.
---

# Getting started with Allure Pytest

[![Allure Pytest pypi latest version](https://img.shields.io/pypi/v/allure-pytest?style=flat "Allure Pytest pypi latest version")](https://pypi.org/project/allure-pytest/)

Generate beautiful HTML reports using [Allure Report](https://allurereport.org/docs/) and your [pytest](https://pytest.org/) tests.

Info:
Check out the example project at [github.com/allure-examples/pytest-pip](https://github.com/allure-examples/pytest-pip) to see Allure Pytest in action.

## Setting up

### 1. Prepare your project

These instructions depend on whether [you have some tests already](#adding-allure-pytest-to-an-existing-project) or [you're just starting a project](#starting-a-new-project-with-allure-pytest).

#### Adding Allure Pytest to an existing project

If you have some pytest tests, and you want to be able to use Allure Report with them:

1. Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the [installation instructions](/docs/v2/install/).

1. Open a terminal and go to the project directory. For example:

   ```bash
   cd /home/user/myproject
   ```

1. If necessary for your system configuration, activate the virtual Python environment for your project. For example, if the project uses a `venv` environment, the command to activate it may look like this:

   ```bash
   source .venv/bin/activate
   ```

   This step is not necessary if you are using the system Python environment.

1. Install the Allure Pytest integration.

   ```bash
   pip install allure-pytest
   ```

#### Starting a new project with Allure Pytest

If you are starting a new project which will contain pytest tests:

1. Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the [installation instructions](/docs/v2/install/).

1. Create a directory for the project.

1. Open a terminal and go to the project directory. For example:

   ```bash
   cd /home/user/myproject
   ```

1. If necessary, create and activate virtual Python environment for your project. For example, the commands to create and activate a `venv` environment may look like this:

   ```bash
   python -m venv .venv
   source .venv/bin/activate
   ```

   This step is not necessary if you are going to use the system Python environment in the project.

1. Write your tests, following the official pytest documentation and, optionally, using some extended features provided by Allure Pytest, see [Writing tests](#writing-tests).

### 2. Run tests

When running your tests, specify a path for the test results directory in the `--alluredir` command-line argument. For example:

```bash
python -m pytest --alluredir allure-results
```

This will save necessary data into the test results directory. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.

### 3. Generate a report

Finally, convert the test results into an HTML report. This can be done by one of two commands:

- `allure generate` processes the test results and saves an HTML report into the `allure-report` directory. To view the report, use the `allure open` command.

- `allure serve` creates the same report as `allure generate`, then automatically opens the main page of the report in a web browser.

## Writing tests

The Allure Pytest integration not only collects the data provided by pytest's standard features, but also provides additional features for writing even better tests. This section lists the most notable ways to improve your tests, using both pytest's and Allure Pytest's features.

With Allure Pytest, you can:

- provide [description, links and other metadata](#specify-description-links-and-other-metadata),
- [organize tests](#organize-tests) into hierarchies,
- divide the test into smaller, easier-to-read [test steps](#divide-a-test-into-steps),
- describe parameters used when running [parametrized tests](#describe-parametrized-tests),
- add readable titles to [fixtures](#describe-fixtures),
- make the test save [screenshots and other files](#attach-screenshots-and-other-files) during execution,
- [select which tests to run](#select-tests-via-a-test-plan-file) via a test plan file.

### Specify description, links and other metadata

There is a lot of [metadata](/docs/v2/readability/#description-links-and-other-metadata) you can add to each test so that it would appear in the report. See the [reference](/docs/pytest-reference/#metadata) for more details.

For each of the metadata fields, there are multiple ways to assign it.

- Call an Allure's function inside the test function's body. This way is called “dynamic” because it allows you to construct strings and other values at runtime before passing to the methods.
- Use an Allure's function as a decorator on the test function or a test class.
- Add an Allure's function as a pytest mark to a module or a package (see the [Marking whole classes or modules](https://docs.pytest.org/en/7.3.x/example/markers.html#marking-whole-classes-or-modules) section in pytest documentation).

Tip:
If you set the fields using the decorators or pytest marks, you can then select and run tests by some metadata fields, see [Options affecting test selection](/docs/pytest-configuration/#options-affecting-test-selection).

**Decorators API:**
```python
import allure

@allure.title("Test Authentication")
@allure.description("This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.")
@allure.tag("NewUI", "Essentials", "Authentication")
@allure.severity(allure.severity_level.CRITICAL)
@allure.label("owner", "John Doe")
@allure.link("https://dev.example.com/", name="Website")
@allure.issue("AUTH-123")
@allure.testcase("TMS-456")
def test_authentication():
    ...
```

**Runtime API:**
```python
import allure

def test_authentication():
    allure.dynamic.title("Test Authentication")
    allure.dynamic.description("This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.")
    allure.dynamic.tag("NewUI", "Essentials", "Authentication")
    allure.dynamic.severity(allure.severity_level.CRITICAL)
    allure.dynamic.label("owner", "John Doe")
    allure.dynamic.link("https://dev.example.com/", name="Website")
    allure.dynamic.issue("AUTH-123")
    allure.dynamic.testcase("TMS-456")
    ...
```

### Organize tests

As described in [Improving navigation in your test report](/docs/v2/navigation/), Allure supports multiple ways to organize tests into hierarchical structures. Allure Pytest provides functions to assign the relevant fields to tests either by adding decorators or marks or by calling the “dynamic” functions (same as for the [metadata fields](#specify-description-links-and-other-metadata)).

To specify a test's location in the [behavior-based hierarchy](/docs/v2/navigation/#behavior-based-hierarchy):

**Decorators API:**
```python
import allure

@allure.epic("Web interface")
@allure.feature("Essential features")
@allure.story("Authentication")
def test_authentication():
    ...
```

**Runtime API:**
```python
import allure

def test_authentication():
    allure.dynamic.epic("Web interface")
    allure.dynamic.feature("Essential features")
    allure.dynamic.story("Authentication")
    ...
```

To specify a test's location in the [suite-based hierarchy](/docs/v2/navigation/#suite-based-hierarchy):

**Decorators API:**
```python
import allure

@allure.parent_suite("Tests for web interface")
@allure.suite("Tests for essential features")
@allure.sub_suite("Tests for authentication")
def test_authentication():
    ...
```

**Runtime API:**
```python
import allure

def test_authentication():
    allure.dynamic.parent_suite("Tests for web interface")
    allure.dynamic.suite("Tests for essential features")
    allure.dynamic.sub_suite("Tests for authentication")
    ...
```

A test's location in the [package-based hierarchy](/docs/v2/navigation/#package-based-hierarchy) is defined by the fully qualified named of the classes and functions they are declared in, with common prefixes shown as parent packages.

### Divide a test into steps

Allure Pytest provides two ways of [creating steps and sub-steps](/docs/steps/): “decorated steps” and “context steps”, both implemented by [`allure.step()`](/docs/pytest-reference/#test-steps).

**Decorated steps:**
```python
import allure

def test_example():
    steps = Steps()
    steps.step1()
    steps.step2()

class Steps:
    @allure.step("Step 1")
    def step1(self):
        ...

    @allure.step("Step 2")
    def step2(self):
        ...
```

**Context steps:**
```python
import allure

def test_example():
    with allure.step("Step 1"):
        ...

    with allure.step("Step 2"):
        ...
```

### Describe parametrized tests

When using the [parametrized tests](/docs/v2/readability/#parametrized-tests) pattern, Allure Pytest automatically adds the parameter values to the test report. Allure supports all the methods of parametrizing tests supported by pytest, including [declaring parameters for fixtures](https://docs.pytest.org/en/latest/how-to/fixtures.html#fixture-parametrize).

Additionally, you can:

- incorporate a parameter's value into the test titles, see [`title()`](/docs/pytest-reference/#title);
- manually add a pseudo-parameter, see [`parameter()`](/docs/pytest-reference/#parametrized-tests).
- override a parameter's value with a more readable representation, see [`parameter()`](/docs/pytest-reference/#parametrized-tests);

```python
from os.path import basename, expanduser

import allure
import pytest

@pytest.mark.parametrize("login", [
    "johndoe",
    "johndoe@example.com",
])
def test_authentication(login):
    ...

def test_authentication_with_empty_login():
    allure.dynamic.parameter("login", "(empty)")
    ...

@pytest.mark.parametrize("ssh_key", [
    expanduser("~/.ssh/id_rsa1"),
    expanduser("~/.ssh/id_rsa2"),
    expanduser("~/.ssh/id_rsa3"),
])
def test_authentication_with_ssh_key(ssh_key):
    allure.dynamic.parameter("ssh_key", basename(ssh_key))
    ...
```

Tip:

For more information, you can consult the guide on [Pytest parameterization](/docs/guides/pytest-parameterization/).

### Describe fixtures

The pytest's concept of **fixtures** provides a way for each test to define features it requires by just specifying function arguments with their names. Some fixtures are provided by pytest itself, others can be provided by additional libraries or your own code. See the [pytest documentation](https://docs.pytest.org/en/latest/explanation/fixtures.html) for more details.

When displaying a test in a test report, Allure displays operations related to fixtures similarly to how it displays [test steps](#divide-a-test-into-steps). To make it easier to understand a fixture's purpose, you can specify its title using the [`@allure.title()`](/docs/pytest-reference/#title) decorator.

```python
import allure
import pytest

@pytest.fixture()
@allure.title("Prepare for the test")
def my_fixture():
    ...  # set up
    yield
    ...  # tear down

def test_with_my_fixture(my_fixture):
    ...
```

Images: /images/python/pytest/fixtures-allure3.png, /images/python/pytest/fixtures-allure2.png

### Attach screenshots and other files

Tip:

Check out our guides for screenshots and videos in Pytest using Selenium or Playwright plugin:

- [Pytest & Selenium: screenshots](/docs/guides/pytest-selenium-screenshots/)
- [Pytest & Playwright: screenshots](/docs/guides/playwright-pytest-screenshots/)
- [Pytest & Playwright: videos](/docs/guides/pytest-playwright-video/)

You can [attach any sorts of files](/docs/attachments/) to your Allure report. For example, a popular way to make a report easier to understand is to attach a screenshot of the user interface at a certain point. To do so, use [`allure.attach()`](/docs/pytest-reference/#attach) or [`allure.attach.file()`](/docs/pytest-reference/#attachfile).

```python
import allure
from pathlib import Path

def test_attach(page):
    page.goto("https://example.com/")
    png_bytes = page.screenshot()
    allure.attach(
        png_bytes,
        name="full-page",
        attachment_type=allure.attachment_type.PNG
    )

def test_attach_file(page):
    page.goto("https://example.com/")
    png_bytes = page.screenshot()
    Path("full-page.png").write_bytes(png_bytes)
    allure.attach.file(
        "full-page.png",
        name="full-page",
        attachment_type=allure.attachment_type.PNG
    )
```

By default, Allure Pytest adds the following data as attached pseudo-files:

| Attachment name | Contents                                                                                                                                                           |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `stdout`        | All data that was written to `sys.stdout`, e.g., via [`print(...)`](https://docs.python.org/3/library/functions.html#print).                                       |
| `stderr`        | All data that was written to `sys.stderr`, e.g., via [`print(..., file=sys.stderr)`](https://docs.python.org/3/library/functions.html#print).                      |
| `log`           | All data that was logged to the standard logging mechanism, e.g., via [`logging.debug(...)`](https://docs.python.org/3/library/logging.html#logging.Logger.debug). |

This behavior can be disabled by the [`--allure-no-capture`](/docs/pytest-configuration/#allure-no-capture) option.

### Select tests via a test plan file

If the `ALLURE_TESTPLAN_PATH` environment variable is defined and points to an existing file, pytest will only run tests listed in this file.

Here's an example of running tests according to a file named `testplan.json`:

**MacOS/Linux:**
```bash
export ALLURE_TESTPLAN_PATH=testplan.json
python -m pytest
```

**Windows:**
```powershell
$Env:ALLURE_TESTPLAN_PATH = "testplan.json"
python -m pytest
```

### Environment information

For the main page of the report, you can collect various information about the environment in which the tests were executed.

For example, it is a good idea to use this to remember the OS version and Python version. This may help the future reader investigate bugs that are reproducible only in some environments.

Images: /images/python/environment-allure3.png, /images/python/environment-allure2.png

To provide environment information, put a file named `environment.properties` into the `allure-results` directory after running the tests. See the example in [Environment file](/docs/how-it-works-environment-file/).

Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using [Parametrized tests](#describe-parametrized-tests).
