Betamax
raw JSON → 0.9.0 verified Fri May 01 auth: no python
A VCR imitation for python-requests that records HTTP interactions and replays them during tests. Current version 0.9.0; release cadence is low (last release 2023).
pip install betamax Common errors
error FileNotFoundError: [Errno 2] No such file or directory: 'cassettes/example.json' ↓
cause Default cassette_library_dir is 'cassettes/' and it does not exist.
fix
Create a 'cassettes' folder in your working directory, or set cassette_library_dir to an existing path.
error No cassette found for request: GET http://httpbin.org/get ↓
cause The request being made does not match any recorded cassette; maybe headers or body differ.
fix
Ensure that the request (method, URI, headers, body) exactly matches the recorded interaction. Or use 'record_mode' to allow recording.
Warnings
deprecated python-requests < 2.0 are not supported. Betamax requires requests 2.x. ↓
fix Upgrade requests to >=2.0.
gotcha Cassette files are stored in a 'cassettes/' directory by default. Forgetting to create this directory or adjust the path can cause FileNotFoundError. ↓
fix Ensure the cassettes directory exists or set a custom cassette_library_dir.
gotcha Betamax does not match requests purely by URL; it uses request method, URI, and (optionally) headers/body. Mismatches lead to cassette playback failures. ↓
fix Configure matchers via Betamax(matchers=...) or use default Matchers.
Imports
- Betamax wrong
from betamax import BetamaxSessioncorrectfrom betamax import Betamax - recorder wrong
recorder = Betamax(session); recorder.start()correctwith Betamax(session) as recorder:
Quickstart
import requests
from betamax import Betamax
session = requests.Session()
recorder = Betamax(session)
with recorder.use_cassette('example'):
response = session.get('https://httpbin.org/get')
print(response.status_code)