Automatically mock your HTTP interactions to simplify and speed up testing
Recording behavior configuration that determines when HTTP interactions are recorded versus replayed from existing cassettes. Record modes provide fine-grained control over VCR.py's recording strategy for different testing scenarios.
Enumeration defining when VCR will record HTTP interactions to cassettes.
class RecordMode(str, Enum):
"""
Configures when VCR will record to the cassette.
Can be used either as enum values (vcr.mode.ONCE) or as strings ("once").
"""
ALL = "all"
ANY = "any"
NEW_EPISODES = "new_episodes"
NONE = "none"
ONCE = "once"# Alias for RecordMode for backward compatibility
mode = RecordModeValue: "once"
Behavior: Records the first set of requests, then replays them on subsequent runs. Attempting to add new episodes fails.
import vcr
# Using enum
my_vcr = vcr.VCR(record_mode=vcr.mode.ONCE)
# Using string
my_vcr = vcr.VCR(record_mode="once")
@my_vcr.use_cassette('test.yaml')
def test_once_mode():
# First run: records all HTTP interactions
# Subsequent runs: replays recorded interactions
# New requests not in cassette will cause test failure
passValue: "new_episodes"
Behavior: Replays existing interactions from cassette, but records any new requests not found in the cassette.
my_vcr = vcr.VCR(record_mode=vcr.mode.NEW_EPISODES)
@my_vcr.use_cassette('test.yaml')
def test_new_episodes():
# Existing requests: replayed from cassette
# New requests: recorded and added to cassette
# Useful for evolving APIs or adding new test coverage
passValue: "all"
Behavior: Records every request, regardless of whether it already exists in the cassette.
my_vcr = vcr.VCR(record_mode=vcr.mode.ALL)
@my_vcr.use_cassette('test.yaml')
def test_all_mode():
# Every HTTP request is recorded
# Cassette grows with each test run
# Useful for debugging or capturing request variations
passValue: "none"
Behavior: No requests are recorded. Only existing cassette interactions are replayed.
my_vcr = vcr.VCR(record_mode=vcr.mode.NONE)
@my_vcr.use_cassette('test.yaml')
def test_none_mode():
# No recording occurs
# Only existing cassette interactions available
# New requests will cause UnhandledHTTPRequestError
# Useful for ensuring tests don't make unexpected requests
passValue: "any"
Behavior: Special recording mode with specific matching behavior.
my_vcr = vcr.VCR(record_mode=vcr.mode.ANY)
@my_vcr.use_cassette('test.yaml')
def test_any_mode():
# Special matching behavior
# Consult VCR.py documentation for specific ANY mode semantics
passimport vcr
# During initial development - record all interactions
dev_vcr = vcr.VCR(record_mode=vcr.mode.ALL)
# For stable tests - use recorded interactions only
stable_vcr = vcr.VCR(record_mode=vcr.mode.ONCE)
# When adding new features - record new requests
incremental_vcr = vcr.VCR(record_mode=vcr.mode.NEW_EPISODES)
# For CI/production - ensure no unexpected requests
strict_vcr = vcr.VCR(record_mode=vcr.mode.NONE)import os
import vcr
# Select mode based on environment
mode = os.environ.get('VCR_RECORD_MODE', 'once')
my_vcr = vcr.VCR(record_mode=mode)
@my_vcr.use_cassette('test.yaml')
def test_with_env_mode():
# Mode controlled by environment variable
# VCR_RECORD_MODE=all pytest test_file.py
# VCR_RECORD_MODE=none pytest test_file.py
passbase_vcr = vcr.VCR(record_mode=vcr.mode.ONCE)
@base_vcr.use_cassette('test.yaml', record_mode=vcr.mode.NEW_EPISODES)
def test_with_mode_override():
# Override the base VCR's record mode for this specific test
# Useful for individual test customization
passWhen using ONCE mode, attempts to make requests not in the cassette will raise an exception:
import vcr
@vcr.use_cassette('existing.yaml', record_mode='once')
def test_new_request_fails():
# If existing.yaml doesn't contain this request:
# Raises: CannotOverwriteExistingCassetteException
response = requests.get('http://new-endpoint.com')When using NONE mode, requests not in the cassette will raise an error:
@vcr.use_cassette('partial.yaml', record_mode='none')
def test_missing_request_fails():
# If partial.yaml doesn't contain this request:
# Raises: UnhandledHTTPRequestError
response = requests.get('http://missing-endpoint.com')Install with Tessl CLI
npx tessl i tessl/pypi-vcrpy