vcrpy icon indicating copy to clipboard operation
vcrpy copied to clipboard

How to run vcr during a pytest run, but not in production?

Open gustavonmartins opened this issue 3 years ago • 3 comments

I have just installed vcr and I love it. I did some testing with pytest on my software, and its all passing now, but when I put it in production, I saw that I was not getting access to the real API calls that I am making, but instead I only get what is recorded inside the cassete file.

How can I enable vcr to run only during tests?

I have tried pytest-vcr, but it didnt work well for me: In my specific case, I have only one function which directly talks to an API, and this function is called by some functions in the code, which are are also called in other places. I had the impression that I had to annotate all possible direct or indirect calls to that function, which gets quite impractical if the codebase is big.

Is there a way to write something like:

@vcr.use_cassette(active_on="testing_only")
def function_calling_api():
    requests.get(...)

Is this functionaly already existing and only misses some explicit documentation, or would it have to be created?

Thanks!

gustavonmartins avatar Mar 05 '21 12:03 gustavonmartins

I'm new to this tool as well, but this line in your Issue confused me:

I saw that I was not getting access to the real API calls that I am making, but instead I only get what is recorded inside the cassete file.

That is the main point of VCR, and the reason one uses it - to prevent your test runner from talking to the live endpoints. It's supposed to use your cassette instead - that's why you have the cassette. It sounds like you asking for it to do the opposite of what it is designed for?

shacker avatar Mar 11 '21 21:03 shacker

Hey, it like you've put the vcr_use_cassette decorator in your production code. Move it to your test code:

@use_cassette()
def a_test():
    result = function_calling_api()
    assert(result...)

JayBazuzi avatar Mar 25 '21 14:03 JayBazuzi

@JayBazuzi

This actually solved the problem, thanks for that!!

It would be nice if there was a reminder on the readme file about that :)

gustavonmartins avatar Apr 05 '21 12:04 gustavonmartins