vcrpy (version 8.1.1) is a Python library that automatically mocks your HTTP interactions, making tests faster and more reliable by 'recording' network requests and 'replaying' them from a local file (a 'cassette'). It supports various HTTP libraries like `requests`, `aiohttp`, and `httpx`. The library maintains an active development pace with frequent updates and bug fixes, typically releasing new versions every few months.
pip install vcrpyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `vcrpy` as a context manager with `requests` to record and replay HTTP interactions. The `vcr.VCR()` object is configured to store cassettes in a specified directory. The first run will make a real network request and save it; subsequent runs with the same cassette name will replay the recorded response, speeding up tests and isolating them from network fluctuations.
Upgrade Python to version 3.10 or newer.
Ensure `urllib3` is updated to version 2.0.0 or newer (e.g., `pip install 'urllib3>=2'`).
Recreate any `httpx` related cassettes after upgrading. Review `httpx` request patterns if encountering new issues.
If your project uses `boto`, migrate to `boto3` or pin `vcrpy` to a version older than 6.0.0.
After upgrading to v6.0.0 or later, it is recommended to recreate all existing `httpx` cassettes.
Upgrade to v8.1.1 or newer to ensure correct behavior of sync requests in async contexts with `httpx`.
Install the library using pip: `pip install vcrpy`
Install vcrpy with the required extra: `pip install "vcrpy[httpx]"` or `pip install "vcrpy[aiohttp]"`
Install vcrpy with the httpx extra: `pip install "vcrpy[httpx]"`
Provide a path to the cassette file: `@vcr.use_cassette('cassettes/my_cassette.yaml')` or `with vcr.use_cassette('cassettes/my_cassette.yaml'):`Ensure the cassette file exists (e.g., by recording it first with `record_mode='new_episodes'`) or change the `record_mode` to allow recording (e.g., `'once'`, `'new_episodes'`, or `'all'`).