---
title: Integrating videos in Allure Report with Pytest and Playwright
description: The guide describes how you can integrate video attachments in Allure Report with Pytest and Playwright
---

# Integrating videos in Allure Report with Pytest and Playwright

Reading test results should be a quick process, allowing the reader to understand failures without digging into code, running a debugger, or interrupting teammates. To achieve this, test reports must present detailed information in a concise and accessible format. While [attaching screenshots](/docs/attachments/#screenshots) and logs is standard practice, it is sometimes insufficient.

In such cases, recording a video of the test execution can be invaluable. Videos improve issue reproduction, speed up debugging, and help automated test analysis. They also improve documentation accuracy by capturing intermittent bugs.

In this guide, we will set up tests with Pytest, record them using Playwright Pytest, and attach videos to reports in Allure Report.

## 1. Preparation

### Prerequisites

Make sure the following prerequisites are met:

- [Python installed](https://www.python.org/downloads/)
- [Allure Report installed](/docs/v2/install/)

### Dependency List

This guide uses the following packages:

- [Pytest 8.3.4](https://pypi.org/project/pytest/8.3.4/)
- [allure-pytest 2.13.5](https://pypi.org/project/allure-pytest/2.13.5/)
- [Pytest-playwright 0.7.0](https://pypi.org/project/pytest-playwright/0.7.0/)

### Code sample

The complete source code used in this guide is available at [https://github.com/allure-examples/guide-pytest-playwright-video](https://github.com/allure-examples/guide-pytest-playwright-video).

### Setup

To run the examples in this guide:

1. Install [Python](https://www.python.org/downloads/)
1. Install [Allure Report](/docs/v2/install/)
1. Download a fresh project with Pytest from [Allure Start](/start/)
1. Add [the Playwright Pytest plugin](https://playwright.dev/python/docs/intro#installing-playwright-pytest) to your project. For example, if you use `pip`:

**Install Playwright Pytest:**
```shell
pip install pytest-playwright
```

## 2. Recording videos with Playwright Pytest

Playwright Pytest records videos in the browser context and saves them as [WebM](https://en.wikipedia.org/wiki/WebM) files. To start recording manually, you can create a browser context with the [`new_context()`](https://playwright.dev/python/docs/api/class-browser#browser-new-context) method of the `Browser` object and pass the [`record_video_dir`](https://playwright.dev/python/docs/api/class-browser#browser-new-context-option-record-video-dir) parameter for it, for example:

```python
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # Start a browser
    browser = p.chromium.launch()
    # Initialising a browser context with video recording
    context = browser.new_context(record_video_dir="path/to/videos")

    page = context.new_page()
    page.goto("https://playwright.dev")
```

## 3. Automatic videos on failure

To automatically save the video in case of a test failure, set the [`--video`](https://playwright.dev/python/docs/test-runners#cli-arguments) option to `retain-on-failure` in your project's [Pytest configuration](https://pytest.org/en/latest/reference/customize.html#command-line-options-and-configuration-file-settings). For example, if you use `pyproject.toml` as a configuration file, add the following:

```toml
[tool.pytest.ini_options]
...
addopts = [
    ...
    "--video", "retain-on-failure",
]
```

For each test, Playwright Pytest creates a subdirectory in the `test-results` directory and saves the related video there.

## 4. Attaching videos to an Allure Report

While capturing videos is beneficial for diagnosing test failures, viewing them separately can be inefficient. When test data is scattered across multiple locations, identifying and resolving issues becomes more time-consuming.

To streamline this process, videos should be directly attached to Allure Reports, ensuring all relevant test information is consolidated in one place for efficient analysis and troubleshooting.

Allure Report displays videos within the test where they were recorded, alongside error messages and other attachments, providing everything you need to analyze test results effectively.

![Video Attachment In Allure Report With Pytest](/images/guides/pytest-playwright-video/pytest_playwright_video_attachment.png "Video Attachment In Allure Report With Pytest")

To add a local video file to Allure Report, use the [`allure.attach.file()`](/docs/pytest-reference/#reading-attachments-from-files) function:

```python
import allure

allure.attach.file(
    "file_name.webm",
    name="video attachment",
    attachment_type=allure.attachment_type.WEBM
)
```

To attach a local video automatically on every test failure, you can write a hook in the `conftest.py` file in your project's test root directory. Assuming you set the `--video` configuration option as described in [**Automatic videos on failure**](#_3-automatic-videos-on-failure) section, the hook may look up the saved videos and attach them to Allure Report. For example:

```python
# Contents of conftest.py
import pathlib
import allure
import pytest

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(item, nextitem):
    yield

    try:
        # Get the output directory for the test
        artifacts_dir = item.funcargs.get("output_path")
        if artifacts_dir:
            artifacts_dir_path = pathlib.Path(artifacts_dir)

            if artifacts_dir_path.is_dir():
                for file in artifacts_dir_path.iterdir():
                    # Find the video file and attach it to Allure Report
                    if file.is_file() and file.suffix == ".webm":
                        allure.attach.file(
                            file,
                            name=file.name,
                            attachment_type=allure.attachment_type.WEBM,
                        )

    except Exception as e:
        print(f"Error attaching video: {e}")
```

## 5. Other attachment types

Allure Report can also handle other types of attachments - images, text, tables, URI lists, or documents (XML, JSON, YAML). For example:

```python
allure.attach(
    response_content,
    name="response content",
    attachment_type=allure.attachment_type.XML
)
```

Allure Pytest makes some attachments automatically:

1. everything passed to `sys.stdout` - e.g. `print(...)`
2. everything passed to `sys.stderr` - e.g. `print(..., file=sys.stderr)`
3. logging, e.g. `logging.warning(...)`

These outputs are attached as pseudo-files to the report:

![Automatic Attachments In Allure Report With Pytest](/images/guides/pytest-screenshots/attachment_pytest_auto.png "Automatic Attachments In Allure Report With Pytest")

If you don’t want these automatic Pytest attachments to appear in the report, use the [`--allure-no-capture`](/docs/pytest-configuration/#allure-no-capture) option.

If you want to learn how attach screenshots, consult the guide for [attachments with Pytest and Playwright](/docs/guides/playwright-pytest-screenshots/) and the documentation for [attachment types](/docs/attachments/).

## 6. Conclusion

With Pytest and Playwright, you can record videos of your tests to provide maximum visibility of your tests’ execution. Allure Report allows you to view those videos with other attachment types (screenshots, text, documents, tables) inside the respective test case, providing the test reader with the maximum amount of data to resolve failures.
