kiwicom/pytest-recording

[Bug] Using `once` record_mode gives error

paxcodes opened this issue · 7 comments

Problem

Using once gives an error:

vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing cassette ([PATH_OF_CASETTE_FILE]) in your current record mode ('once').
E               No match for the request (<Request (GET) https://postman-echo.com/get?foo1=bar1&foo2=bar2>) was found.
E               No similar requests, that have not been played, found.

I ran this the first time so PATH_OF_CASETTE_FILE shouldn't exist although the error seems to say it exists. I checked the PATH_OF_CASETTE_FILE that the error specifies before I run my test the 2nd time and indeed no such file exists.

To Reproduce

import pytest
import requests

@pytest.mark.vcr(record_mode="once")
def test_vcr():
    response = requests.get("https://postman-echo.com/get?foo1=bar1&foo2=bar2")
    assert response.status_code == 200

Stacktrace

Click to expand!
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/requests/api.py:76: in get
    return request('get', url, params=params, **kwargs)
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/requests/api.py:61: in request
    return session.request(method=method, url=url, **kwargs)
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/requests/sessions.py:530: in request
    resp = self.send(prep, **send_kwargs)
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/requests/sessions.py:643: in send
    r = adapter.send(request, **kwargs)
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/requests/adapters.py:439: in send
    resp = conn.urlopen(
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/urllib3/connectionpool.py:670: in urlopen
    httplib_response = self._make_request(
../../Library/Caches/pypoetry/virtualenvs/featuredproductsswitch-q2O7GQgj-py3.8/lib/python3.8/site-packages/urllib3/connectionpool.py:417: in _make_request
    httplib_response = conn.getresponse(buffering=True)

Hi @paxcodes

Thank you for reporting this! Indeed it is strange, I'll take a look - a similar code with only VCR.py has different behavior

The problem is, that record_mode passed in the vcr mark is not passed to VCR. I'll fix that

We have this:

def load_cassette(cassette_path, serializer):
    try:
        with open(cassette_path) as f:
            cassette_content = f.read()
    except IOError:
        return [], []
    return deserialize(cassette_content, serializer)

But it actually should raise an error, but it interferes with some other parts - I am taking a deeper look

So, CombinedPersister should raise an error only if no requests / responses were loaded from any cassettes. Will fix it soon

I released a fix in 0.8.1. Let me know how it works for your case

Works perfectly. Thanks once again @Stranger6667!