pytest-recording is a pytest plugin that integrates VCR.py to record and replay HTTP traffic during tests. It aims to make testing code that interacts with external services faster and more reliable by preventing unnecessary network requests. The library uses VCR.py's 'none' recording mode by default to avoid unintentional live network calls. The current version is 0.13.4, and it maintains an active release cadence.
pip install pytest-recordingVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up `pytest-recording` to record and replay HTTP requests using the `@pytest.mark.vcr` decorator. It includes a `vcr_config` fixture, typically placed in a `conftest.py` file or at the top of a test module, to apply global VCR.py configurations like filtering sensitive headers. To run these tests and record the network interactions, use `pytest --record-mode=once your_test_file.py`. The `--record-mode=once` argument is crucial for the initial recording, as `pytest-recording` defaults to `none` mode to prevent accidental network requests. Subsequent runs without this flag will replay from the cassette.
Uninstall `pytest-vcr` using `pip uninstall pytest-vcr` before installing or using `pytest-recording`.
When running tests that require recording or updating cassettes, use `pytest --record-mode=once` (or another appropriate mode like `all`) in your command line.
Use the `vcr_config` fixture or `pytest.mark.vcr` parameters to `filter_headers` and `filter_query_parameters`. For API keys, pass them via environment variables and ensure they are filtered out of the cassette. For example:
`@pytest.fixture(scope='module') def vcr_config(): return {'filter_headers': ['Authorization']}`Be aware of the configuration priority: `vcr_config` fixture (lowest priority) < `pytest.mark.vcr` (broad scope) < `pytest.mark.vcr` (narrow scope, highest priority). Explicitly define parameters where you need specific overrides.
Change the recording mode to allow recording (e.g., `new_episodes` or `all`) using a pytest marker: `@pytest.mark.vcr(record_mode='new_episodes')`.
Ensure the cassette file exists at the expected path, or change the recording mode to one that generates a cassette (e.g., `new_episodes` or `all`) using a pytest marker: `@pytest.mark.vcr(record_mode='new_episodes')`.
Install the library using pip: `pip install pytest-recording`.
Ensure `pytest-recording` is installed (`pip install pytest-recording`) and the `vcr` fixture is correctly passed as an argument to your test function: `def test_example(vcr): ...`.