VCR.py
ActiveOverview
VCR.py is a Python library that records HTTP interactions made during test execution and replays them on subsequent runs, eliminating the need for actual HTTP requests during testing. It is designed for developers testing code that makes HTTP requests to external APIs or services. The tool improves test reliability by removing dependencies on external services, enabling offline testing, and ensuring deterministic test results regardless of API availability or changes.
Key Features
- HTTP Request Recording - Records HTTP requests and responses from the first test run and serializes them to YAML cassette files
- Request Replay - Intercepts and replays recorded HTTP interactions on subsequent test runs without making actual network requests
- Offline Testing - Enables tests to run without internet connectivity by using previously recorded responses
- Deterministic Tests - Ensures consistent, repeatable test results independent of external service availability or API changes
- Cassette Management - Automatically detects missing cassette files and re-records interactions when needed
- Sensitive Data Filtering - Can obscure API tokens and passwords in cassette files for security
- Recording Modes - Supports multiple recording modes including 'once' (default), 'new_episodes', and 'none' for different testing scenarios
- Cassette Inspection - Allows assertions on recorded requests and responses to verify expected API interactions
Pricing
| Plan | Price | Includes |
|---|---|---|
| Open Source | Free | Full access to VCR.py library, community support, all core features |
Platforms & Requirements
VCR.py runs on Linux, macOS, and Windows as a Python library. It requires Python 3.6 or later and integrates with Python testing frameworks. The library works with HTTP libraries including requests, urllib, httplib, and others that make HTTP connections.
Integrations & Ecosystem
- Python requests library
- urllib
- httplib
- pytest
- unittest
- Mock libraries
- YAML serialization
- HTTP request interception
Alternatives
| App | Difference |
|---|---|
| responses | Simpler mock library for requests that doesn't record interactions; requires manual response definition |
| betamax | Similar recording library but less actively maintained; originally inspired by VCR |
| httpretty | HTTP mocking library that patches socket connections; doesn't provide cassette-based recording |
| unittest.mock | Python standard library mocking tool; requires manual setup and doesn't record real interactions |
Reputation
VCR.py is widely recognized as a reliable and practical solution for testing HTTP-dependent code in Python. It is praised for reducing test execution time, improving reliability by eliminating external service dependencies, and simplifying test maintenance through automatic cassette management. The library is actively maintained and has become a standard tool in Python testing workflows. Some users note that cassette files require version control management and can become large with extensive API interactions, but these are considered minor trade-offs for the benefits provided.
Sources (8)
- https://pypi.org/project/vcrpy/1.7.1/
- https://vcrpy.readthedocs.io
- http://jakesen.github.io/python/testing/2017/03/27/testing-in-python-with-vcrpy.html
- https://vcrpy.readthedocs.io/en/latest/advanced.html
- https://www.pluralsight.com/resources/blog/guides/explore-python-libraries-speed-up-http-tests-with-vcrpy
- https://dev.to/00geekinside00/recording-and-replaying-http-interactions-with-ease-a-guide-to-vcrpy-1c70
- https://punksecurity.co.uk/blog/using_vcrpy/
- https://github.com/vcr/vcr