kiwicom / pytest-recording

A pytest plugin that allows recording network interactions via VCR.py
MIT License
425 stars 34 forks source link

[Bug] Using `once` record_mode gives error #47

Closed paxcodes closed 4 years ago

paxcodes commented 4 years ago

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! ```sh ../../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) ```
Stranger6667 commented 4 years ago

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

Stranger6667 commented 4 years ago

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

paxcodes commented 4 years ago

I believe it also does not work when I use the vcr_config fixture to set the record_mode to 'once'

On Fri, Jun 12, 2020, 7:04 AM Dmitry Dygalo notifications@github.com wrote:

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kiwicom/pytest-recording/issues/47#issuecomment-643287425, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADIDWNTUDBB7FJ4CDKQM56TRWIYVNANCNFSM4N2YC2LQ .

Stranger6667 commented 4 years ago

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

Stranger6667 commented 4 years ago

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

Stranger6667 commented 4 years ago

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

paxcodes commented 4 years ago

Works perfectly. Thanks once again @Stranger6667!