How to use `before_record` and another advanced features?
Closed this issue · 1 comments
I try to imitate this behaviour. How can I do it with pytest-recording?
def before_record_cb(request):
if KB_URL in request.uri:
request.uri = request.uri.replace(KB_URL, 'kb.com')
if (b:=request.body) and KB_URL in b:
request.body = request.body.replace(KB_URL, 'kb.com')
return request
def clean_response(response):
if KB_URL in (b:=str(response['body'])):
response['body'] = b.replace(KB_URL, 'kb.com')
return response
def add_subpath(func):
return str(Path(func.__module__, func.__name__))
my_vcr = vcr.VCR(
before_record_request=before_record_cb,
before_record_response=clean_response,
path_transformer=VCR.ensure_suffix('.yaml'),
func_path_generator=add_subpath,
cassette_library_dir='cassettes'
)
By the way, I do this from your docs and it doesn't work. I see debugger breakpoint into pytest_recording_configure
, but not in jurassic_matcher
def jurassic_matcher(r1, r2):
assert r1.uri == r2.uri and "JURASSIC PARK" in r1.body, \
"required string (JURASSIC PARK) not found in request body"
def pytest_recording_configure(config, vcr):
vcr.register_matcher("jurassic", jurassic_matcher)
By the way, I do this from your docs and it doesn't work. I see debugger breakpoint into pytest_recording_configure, but not in jurassic_matcher
def jurassic_matcher(r1, r2):
assert r1.uri == r2.uri and "JURASSIC PARK" in r1.body,
"required string (JURASSIC PARK) not found in request body"def pytest_recording_configure(config, vcr):
vcr.register_matcher("jurassic", jurassic_matcher)
I've understood how it works. You just should do if globaly then
@pytest.fixture(scope="session")
def vcr_config():
return {
"match_on": ["jurassic"],
"record_mode": "once"
}
It is important to have this record_mode
and already done casset of request.
Would be good to mention this in the docs.
And for before_record
you can do
@pytest.fixture(scope="session")
def vcr_config():
return {
"before_record_request": before_record_cb,
"before_record_response": clean_response,
}