kiwicom / pytest-recording

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

Use the plugin on the autouse fixture #12

Closed dmfigol closed 4 years ago

dmfigol commented 5 years ago

Is there a way to use this plugin on the autouse fixture? Scenario: the test introduces a side effect to HTTP service, so it would be good to have autouse setup/teardown which removes this side-effect allowing the whole test suite to run properly when I delete all cassettes and record all of them.

Stranger6667 commented 5 years ago

Hej! Marks could be added dynamically, this one could work

import pytest

def pytest_collection_modifyitems(items):
    for item in items:
        # maybe some condition here to add marks only if that auto use fixture is present. If it is somewhere lower in the plugin hierarchy 
        item.add_marker(pytest.mark.vcr())
Stranger6667 commented 5 years ago

However there are some alternatives, will respond in the evening

Stranger6667 commented 5 years ago

The plugin has an autouse fixture vcr that relies on the presence of pytest.mark.vcr marks. If a single mark is applied to the current test, then a VCR cassette will be used.

But it is possible to reuse some internal code in another fixture with different conditions to run this use_cassette function:

@pytest.fixture(autouse=True)
def my_vcr(vcr_cassette_dir, record_mode):
    # could be some custom condition here
    config = {}  # custom config
    vcr_markers = [
        (
            ("cassette1.yaml", "cassette2.yaml"),  # cassettes names
            None,  # Optional pytest Mark instance
        )
    ]
    with use_cassette(vcr_cassette_dir, record_mode, vcr_markers, config) as cassette:
        yield cassette

But I didn't test how this code will interact with the rest of the plugin logic. Maybe it will make sense to expose this part with the more convenient interface than this one, not sure. What do you think? Will the options cover your use cases? I'd be happy to know your feedback.

Regards, Dmitry