Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify use_cassette #23

Closed
dazza-codes opened this issue May 9, 2019 · 7 comments
Closed

How to specify use_cassette #23

dazza-codes opened this issue May 9, 2019 · 7 comments

Comments

@dazza-codes
Copy link

dazza-codes commented May 9, 2019

When there are many tests using the same request/response, what's the best way to ensure they all use the same cassette? The vcr.use_cassette API clearly provides an option to name a cassette file. Is this exposed in pytest-vcr? e.g.

@pytest.mark.vcr(vcr_cassette_name='custom')

# or?

@pytest.mark.vcr(use_cassette='cassettes/custom.yaml')

Is this already noted in the read-the-docs? Where is it explained?

It seems like adding a decorator for VCR to a pytest-fixture does not work, or does it? If so, how?

@ktosiek
Copy link
Owner

ktosiek commented May 12, 2019

Currently passing the cassette name into the marker is not supported.

If the tests are grouped somehow (say, by class) you could override vcr_cassette_name for that scope:

@pytest.mark.vcr
class CommonCassetteTests:
    @pytest.fixture
    def vcr_cassette_name(self):
        return 'custom'

    def test_one(self):
        ...

    def test_two(self):
        ...

Otherwise you can use the vcr fixture to get a current VCR object:

# Note: don't mix the marker with explicit usage of "vcr"
def test_common1(vcr):
    with vcr.use_cassette('common'):
        ...

def test_common2(vcr):
    with vcr.use_cassette('common'):
        ...

I'm not sure what you mean by adding a decorator for VCR - could you rephrase, or provide an example of what you've tried?

@dazza-codes
Copy link
Author

dazza-codes commented May 14, 2019

The latter option worked OK within a pytest fixture, e.g.

@pytest.fixture()
def vcr_fixture(uri, query_params) -> str:
    with vcr.use_cassette("tests/cassettes/vcr_fixture.yaml"):
        response = request.get(uri, query_params)
        return response

(Maybe it should be a pytest yield fixture instead, but it works as is.)

The hope for this feature request is that the name of the cassette could be derived from the name of the fixture function. I'm not familiar with the nested decorators for pytest fixtures, but the suggestion might be something like:

@pytest.mark.vcr
@pytest.fixture
def vcr_fixture():
    pass # TODO do stuff with HTTP

This would maintain a tests/cassettes/vcr_fixture.yaml cassette (for the fixture function).

@ktosiek
Copy link
Owner

ktosiek commented May 20, 2019

Marking a fixture like that seems to be unsupported by pytest: pytest-dev/pytest#3664
I don't have any ideas for a better API, and I think the current one is OK (even if not ideal). Do you have any other ideas that might work?

@valgur
Copy link
Contributor

valgur commented May 21, 2019

How about

@pytest.fixture()
def shared_cassette(vcr):
    with vcr.use_cassette("tests/cassettes/vcr_fixture.yaml"):
        yield

def test_something(shared_cassette):
    pass # TODO do stuff with HTTP

Also, as a side note, setting cassette_library_dir='tests/cassettes/' in the vcr configuration would probably be a good idea.

@dazza-codes
Copy link
Author

If it's a limitation of pytest, it's too much to ask. Thanks for the tips, close at will.

@ktosiek
Copy link
Owner

ktosiek commented May 22, 2019

Closing this, but I'm still open to ideas about better API for using VCR in fixtures.

@valgur is right about overriding cassette_library_dir if your fixture is shared between files in multiple directories, otherwise tests in each directory might end up with separate cassette.

@ktosiek ktosiek closed this as completed May 22, 2019
@dazza-codes
Copy link
Author

Might be fixed by #25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants