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
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.
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.

Record and replay HTTP requests using Betamax with a requests.Session.

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)