responses / VCR "Connection refused" - Cassette Not Matched in CI
Libraries like responses and vcr.py intercept HTTP and replay registered/recorded interactions. When a request does not match any registered mock or recorded cassette, the call falls through (or is blocked), surfacing as a connection error in CI where real network is off.
What this error means
A test using responses or vcr.py fails with ConnectionError, Connection refused, or CannotOverwriteExistingCassetteException because the outgoing request did not match a registered mock or a recorded interaction. It passed locally where the cassette existed or the network was reachable.
vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing
cassette ('cassettes/test_api.yaml') in your current record mode ('none').
No match for the request (GET https://api.example.com/v2/items?page=2) was found.Common causes
Request does not match any mock/cassette
A changed URL, query string, header, or body means the request no longer matches what was registered or recorded, so the library cannot replay it.
Cassette missing or record mode "none"
In CI with record mode none (replay-only), a missing or stale cassette has nothing to serve, and the real network is unavailable.
How to fix it
Register a matching mock (responses)
Add the exact method/URL the code calls so the request is intercepted.
import responses
@responses.activate
def test_items():
responses.get("https://api.example.com/v2/items", json={"items": []})
fetch_items()Record or update the cassette (vcr.py)
Re-record against a controlled endpoint, then commit the cassette and run replay-only in CI.
# record once locally
VCR_RECORD_MODE=once pytest tests/test_api.py
# CI replays only:
# @vcr.use_cassette("cassettes/test_api.yaml", record_mode="none")How to prevent it
- Commit cassettes and run replay-only (
record_mode="none") in CI. - Register mocks for every outbound call the test path makes.
- Configure match-on to ignore volatile request attributes.