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: booloptionally forces recording (True) or replay (False); when omitted, the active pytest mode is used (pytest --recordfor recording).file: Pathoptionally sets the pickle file used to save or load that test's calls.patch_replay(target)patches the dependency named by the dotted import pathtarget: str, recording its calls during recording and replaying them later.recording()returns whether the current run is recording. Thereplay_recordingpytest 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.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest_record_replay-1.1.0.tar.gz | 56.3 kB | With provenance | Details for pytest_record_replay-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_record_replay-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for pytest_record_replay-1.1.0-py3-none-any.whl |
Total release size: 95.3 kB
Release files / pytest_record_replay-1.1.0.tar.gz
| Download URL | pytest_record_replay-1.1.0.tar.gz |
|---|---|
| Size | 56.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a74787ac0075f3462dc69a41ab57c5fe2b5118e1ca638590764472656d1be83c
|
|
BLAKE2b-256 checksum How to use checksums |
6fd8ab1220e4b5f40e8eae3928f7d71612a43f4ea62ffc4dd99f41740ec48a09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 17, 2026.
Transparency logRelease files / pytest_record_replay-1.1.0-py3-none-any.whl
| Download URL | pytest_record_replay-1.1.0-py3-none-any.whl |
|---|---|
| Size | 39.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b2cf226c5efa4fd308744f8fbf1ca1f382ea97c0d8aa589fdbc3dda811f9f60
|
|
BLAKE2b-256 checksum How to use checksums |
a8e05820aa57d67799b9b4f54879434cf60013f15bfa10066cfbdf4f6f4b5966
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 17, 2026.
Transparency logSecurity for pytest-record-replay 1.1.0
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.
Attestation for pytest_record_replay-1.1.0.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 Oct 17, 2026.
Transparency logA 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.