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

[FEATURE] Record pytest fixtures #76

Open
NotBigSimon opened this issue Aug 26, 2021 · 6 comments
Open

[FEATURE] Record pytest fixtures #76

NotBigSimon opened this issue Aug 26, 2021 · 6 comments

Comments

@NotBigSimon
Copy link

Is your feature request related to a problem? Please describe.
In my project i am using fixtures with scope="session" and the requests that are made in the fixture setup are not being recorded, neither in the test casettes, nor in a separate casette, when i mark the fixture with @pytest.mark.vcr

Describe the solution you'd like
I would like it to be possible to decorate fixtures with @pytest.mark.vcr() with the result of a casette being recorded for the individual fixtures, such taht i can use pytest-recording to record fixtures with scope="session"

Describe alternatives you've considered
if i use scope="function" the fixture setup requests are recorded in each test casette and the offline testing works.

@NotBigSimon NotBigSimon changed the title [FEATURE] [FEATURE] Record pytest fixtures Aug 26, 2021
@joshtemple
Copy link

What could be done to make pytest-recording work for fixtures? I'd like to use this library for all VCR so we can ensure headers are filtered by default, but we can't currently do that if it doesn't support recording for session-scoped fixtures.

@joshtemple
Copy link

@Stranger6667, I'd be happy to submit a PR to support marking fixtures in this library. Could you please point me in the right direction or any known blockers?

@birkhola
Copy link

birkhola commented Jun 20, 2022

For scope="module" it also does not record requests/responses done in fixtures.

@Stranger6667
Copy link
Collaborator

Hi everybody! Sorry for the delay.

I think that the way to approach this is to use the logic from the _vcr.py module with fixtures of non-"function" scope. For example, make each "session" fixture depend on a separate "vcr_session" fixture that works similarly to vcr. I.e. opens a context manager that records a cassette.

Though, not sure about configuring such a cassette writer - all the cassette naming & other stuff comes either from higher-level fixtures or pytest marks. The latter don't work on fixtures as far as I can tell :( However, there are module-level marks, so it may be used for recording cassettes inside module-level fixtures.

Here is an idea: Make a session-level fixture that works similarly to the vcr one and also is autouse=True, but can only access session-level configuration (e.g. session-level vcr_config fixture). Then disable it inside the vcr fixture, so it is not recoding calls during the test.

Recording module-level fixtures adds an extra layer here but could be done similarly implementation-wise: no access to higher-level configs & disabling the session-level fixture inside.

Important note: We really need to isolate cassettes from different levels during execution. As VCR is mocking transports, I'd assume it is replacing globally-available functions, so cassettes will interfere with each other. So, it might require some bookkeeping with enabling/disabling different cassettes properly.

Those are my current thoughts on this problem, and I didn't check if this approach will actually work (I assume it should). Let me know if you'd like me to elaborate.

@afuetterer
Copy link

Hi @Stranger6667, is there any update on this? I would be really nice to be able to use vcr in fixtures.

@Stranger6667
Copy link
Collaborator

Hi! Unfortunately I don’t have much bandwidth to implement it myself, but will be happy to provide guidance and review a PR

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

No branches or pull requests

5 participants