def use_vcr(request, monkeypatch):
"""
This fixture is applied automatically to any test using the `online` mark. It will record and playback network
sessions using VCR.
The record mode of VCR can be set using the VCR_RECORD_MODE environment variable when running tests.
"""
if VCR_RECORD_MODE == 'off':
yield None
else:
path_segments = [VCR_CASSETTE_DIR]
if request.module is not None:
path_segments.extend(request.module.__name__.split('tests.')[-1].split('.'))
if request.cls is not None:
path_segments.append(request.cls.__name__)
# Take into account the parametrization set by pytest
# to create many tests from a single function with many parameters.
request_keywords = request.keywords.keys()
if 'parametrize' in request_keywords:
try:
param_name = [x for x in request_keywords if x.startswith('_with_')][0]
except IndexError:
param_name = ''
else:
param_name = ''
path_segments.append(request.function.__name__ + param_name + '.yaml')
cassette_path = os.path.join(*path_segments)
filter_query = [('applicationId', 'XXXXXX')]
filter_headers = [('Authorization', 'ESA XXXXXX')]
filter_post = []
online = True
if vcr.record_mode == 'none':
online = False
elif vcr.record_mode == 'once':
online = not os.path.exists(cassette_path)
# If we are going online, check the credentials
if online:
if os.environ.get("RAKUTEN_APP_ID", None) is None:
pytest.skip('need credentials to run this test')
with vcr.use_cassette(path=cassette_path,
filter_query_parameters=filter_query,
filter_headers=filter_headers,
filter_post_data_parameters=filter_post,
decode_compressed_response=True) as cassette:
yield cassette
评论列表
文章目录