vcrpy
vcrpy copied to clipboard
How to run vcr during a pytest run, but not in production?
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!
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?
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
This actually solved the problem, thanks for that!!
It would be nice if there was a reminder on the readme file about that :)