Skip to main content

pytest-record-replay

pytest-record-replay records calls to dependencies during a test run, then replays those calls on later runs. Use it to test code that depends on services or other external behavior without calling those dependencies every time.

Install

python -m pip install pytest-record-replay

After installation, pytest loads the pytest-record-replay plugin automatically. Use pytest --record to record dependency calls. Run pytest without that option to replay them.

To install a local checkout for development, including the test dependencies:

python -m pip install -e ".[test]"

Overview

  • set_replay(record=None, file=None) configures a test's recording/replay behavior without patching a dependency. record: bool optionally forces recording (True) or replay (False); when omitted, the active pytest mode is used (pytest --record for recording). file: Path optionally sets the pickle file used to save or load that test's calls.
  • patch_replay(target) patches the dependency named by the dotted import path target: str, recording its calls during recording and replaying them later.
  • recording() returns whether the current run is recording. The replay_recording pytest fixture provides the same value to a test.

@patch_replay

Import patch_replay and decorate a test with the dotted name of the dependency it uses. The name should refer to where the dependency is looked up by the code under test.

from pytest_record_replay import patch_replay
from myapp import client


@patch_replay("myapp.client.fetch_data")
def test_fetch_data():
    assert client.fetch_data("item-1") == {"id": "item-1"}

When recording, fetch_data runs normally and pytest-record-replay saves its inputs and output. On replay, it returns the saved output and checks that the inputs match. Apply @patch_replay more than once to record or replay multiple dependencies:

@patch_replay("myapp.client.fetch_data")
@patch_replay("myapp.clock.now")
def test_report():
    ...

Use patch_replay as a context manager

Use patch_replay in a with block to patch a dependency for only part of a test. The context manager yields a mock, so you can make assertions about the calls while the patch is active. Decorate the test with @set_replay() so the calls are saved during recording and loaded during replay:

from pytest_record_replay import patch_replay, set_replay
from myapp import client


@set_replay()
def test_fetch_data():
    with patch_replay("myapp.client.fetch_data") as mock_fetch:
        assert client.fetch_data("item-1") == {"id": "item-1"}
        mock_fetch.assert_called_once_with("item-1")

The patch is removed when the with block exits. As with the decorator, use the dotted name where the code under test looks up the dependency.

Use patch_replay as a fixture

If several tests need the same patch, wrap it in a pytest fixture. Yielding the mock keeps the patch active for the test and automatically removes it when the fixture finishes:

import pytest
from pytest_record_replay import patch_replay, set_replay
from myapp import client


@pytest.fixture
def fetch_data_mock():
    with patch_replay("myapp.client.fetch_data") as mock_fetch:
        yield mock_fetch


@set_replay()
def test_fetch_data(fetch_data_mock):
    assert client.fetch_data("item-1") == {"id": "item-1"}
    fetch_data_mock.assert_called_once_with("item-1")

See the functional tests for runnable examples of decorating functions, TestCase, and using the context-manager and fixture patterns.

@set_replay

Use @set_replay to configure recording/replay metadata on a test without patching a dependency itself. Specify record or file to override the current pytest mode or choose a recording file for that test. It is also useful when composing another test wrapper with @patch_replay.

from pathlib import Path
from pytest_record_replay import patch_replay, set_replay
from myapp import client


@patch_replay("myapp.client.fetch_data")
@set_replay(record=True, file=Path("test-data/fetch.pkl"))
def test_fetch():
    assert client.fetch_data("item-1") == {"id": "item-1"}

If record is omitted, set_replay uses the current pytest mode: recording with --record, replaying without it. When using both decorators, place @set_replay closest to the function so it is applied before @patch_replay.

Patching classes

You can also decorate a test class. Each callable method whose name starts with test receives the patch, so the class can share dependency patches across its tests:

@patch_replay("myapp.client.fetch_data")
@set_replay(file=Path("/path/to/my/file.pkl"))
class TestClient:
    def test_fetch_data(self):
        assert client.fetch_data("item-1") == {"id": "item-1"}

Stack class decorators to patch multiple dependencies for each test method.

Check recording mode

Use recording() in Python code or request pytest's replay_recording fixture in a test. Both report whether the current run is in recording mode.

from pytest_record_replay import recording


def test_result():
    result = run_operation()
    assert result is not None
    if recording():
        assert operation_was_called()
def test_result(replay_recording):
    result = run_operation()
    assert result is not None
    if replay_recording:
        assert operation_was_called()

The fixture is useful when a test needs to make assertions that differ between recording and replay runs. The default mode is replay; pass --record to pytest to enable recording.

Metadata

Release files for pytest-record-replay 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-record-replay 1.1.3
File Size Uploaded Provenance Actions
pytest_record_replay-1.1.3.tar.gz 56.8 kB With provenance Details for pytest_record_replay-1.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-record-replay 1.1.3
File Interpreter ABI Platform Actions
pytest_record_replay-1.1.3-py3-none-any.whl Python 3 none any Details for pytest_record_replay-1.1.3-py3-none-any.whl

Total release size: 96.0 kB

Release files / pytest_record_replay-1.1.3.tar.gz

Download URL pytest_record_replay-1.1.3.tar.gz
Size 56.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2b4c01054afe07deb3a44f53738c7c92f574eaefe77ad01b3f7994123ef705a2
BLAKE2b-256 checksum
How to use checksums
35cee19cd44851d6dde0f4604227c8a56ced462f996683bdb462d58837d972a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / pytest_record_replay-1.1.3-py3-none-any.whl

Download URL pytest_record_replay-1.1.3-py3-none-any.whl
Size 39.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d2854175cd29ac5b8e168f9fc306cfa1c7fa97044c8dbef8aee92dd9fb9d8d1
BLAKE2b-256 checksum
How to use checksums
2cf3bc93c28ad9b12a95a13dee0b3b54dc1a39a4696449596f030020019eca66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for pytest-record-replay 1.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytest_record_replay-1.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page