kiwicom / pytest-recording

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

[FEATURE] Provide a way to use pytest_recording without `pytest.mark.vcr` decorator #75

Open Stranger6667 opened 3 years ago

Stranger6667 commented 3 years ago

Is your feature request related to a problem? Please describe. It might be convenient to use _vcr.use_cassette directly for granular control of VCR setup & teardown mechanisms.

Describe the solution you'd like Create a separate function that will not depend on pytest-related types & expose it as a part of the public API. It might be used by _vcr.use_cassette under the hood (or, likely, _vcr.use_cassette will be renamed to something else, and the new function will take this name).

Additional context Requested by @selevit

CharString commented 3 years ago

pytest-vcr has a vcr fixture that gives you the configured VCR object so you can swap cassettes mid-test and test non-safe methods in 'once' recording mode.

def test_content_editing(vcr):
    with vcr.use_cassette('tape 1.yaml'):
        requests.post(...)
        assert request.get(...).json()['title'] == 'default'
    with vcr.use_cassette('tape 2.yaml'):
        request.patch(..., json={'title': 'new'}
        assert request.get(...).json()['title'] == 'new'
jdputsch commented 1 year ago

Along these lines, would find it very useful to do something like this:

@pytest.fixture(scope="module")
def p4_ipv(pi, ipv):
    with vcr.use_cassette("p4_ipv.yaml"):
        ipv = pi.ipv("jeff_putsch.z@11.TRUNK")
    return ipv

Allowing me to use VCR is module or session scope to setup several object that would then be the basis for related sets of tests. Today, I would have to do something like this:

@pytest.mark.vcr()
def test_one():
      with vcr.use_cassette("p4_ipv.yaml"):
           ipv = pi.ipv("jeff_putsch.z@11.TRUNK")
      # now use the ipv object in further code

@pytest.mark.vcr()
def test_two():
      with vcr.use_cassette("p4_ipv.yaml"):
           ipv = pi.ipv("jeff_putsch.z@11.TRUNK")
      # now use the ipv object in further code

Which is less than ideal. I'd like to be able to refactor the common code out into a fixture. The fixture may want to be allowed to be function, module, or session scope.

Thanks,

Jeff.