betamaxpy / betamax

A VCR imitation designed only for python-requests.
https://betamax.readthedocs.io/en/latest/
Other
565 stars 62 forks source link

"global record" #142

Closed cjw296 closed 6 years ago

cjw296 commented 6 years ago

How would I go about using betamax to record requests on all sessions, regardless of where or how they're created?

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/49779945-global-record?utm_campaign=plugin&utm_content=tracker%2F198445&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F198445&utm_medium=issues&utm_source=github).
cjw296 commented 6 years ago

Looks like I've done this in the past by mocking out the Session class where it's used, but I wonder if there's a better way?

sigmavirus24 commented 6 years ago

The library doesn't presently provide that functionality.

Can you expand a bit more on the constraints of what you're testing such that you need this functionality?

cjw296 commented 6 years ago

Looking to test code like this:

def some_func():
   session = Session(...)
   session.get(...)
hroncok commented 6 years ago

I don't think betamax should do that. If you actually really need to do this, than mocking requests.Session is a way. But if that code is yours, use dependency injection, something like:

def some_func(session=None):
   session = session or Session(...)
   session.get(...)
cjw296 commented 6 years ago

@hroncok - we're all entitled to our opinion ;-)

hroncok commented 6 years ago

What I mean is that there are techniques how to achieve this kind of thing and implement the support for this inside betamax would be out of it's scope. Do you think that betamax should be able to mock all sessions?

cjw296 commented 6 years ago

It would be a nice feature to have...

sigmavirus24 commented 6 years ago

@cjw296 I tend to agree with @hroncok that this isn't really appropriate for the library. The design vision has been pretty consistent. I definitely think a helper for global patching would be valuable to others.