-
Notifications
You must be signed in to change notification settings - Fork 71
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
Unexpected interaction with requests-cache #182
Comments
Hi there,
There's now a section in the docs on how to safely combine multiple libraries that extend The solution described there is what |
Hmm, that's interesting. I'm really interested in the description on the requests-cache documentation that requests-mock is different. In a way I guess we are, there is no such thing as a What i can't wrap my head around at this point in the night is why it still doesn't call us? Technically we are patching Is it something to do with pytest? The following seems to work: import requests
import requests_cache
import requests_mock
import unittest
url = "https://www.test.org"
content = "foo"
url2 = "https://www.test.com/testing"
content2 = "bar"
url3 = "https://www.testing.com/test"
content3 = "foobar"
class Testmytest(unittest.TestCase):
def setUp(self):
super(Testmytest, self).setUp()
requests_cache.clear()
def test_cache_mock_first(self):
with requests_mock.mock() as m:
m.get(url2, text=content2)
with requests_cache.enabled(backend='memory'):
self.assertEqual(content2, requests.get(url2).text)
def test_cache_mock_last(self):
with requests_cache.enabled(backend='memory'):
with requests_mock.mock() as m:
m.get(url3, text=content3)
self.assertEqual(content3, requests.get(url3).text)
if __name__ == '__main__':
unittest.main() I know there are a number of requests libraries that all share this space now. I'd like to integrate as nicely as possible so maybe we do refactor this for a new major version, but if there are any ideas as to what's going on here and how to improve it we will. Can i make a suggestion to @JWCook though, I think you want to have an option to disable requests-cache for running in unit tests. Even trying to figure out what was happening here i found that requests-cache defaulted to writing a |
In general, yes, I think disabling requests-cache for unit tests is the best option. There are a few options for that, including:
Do you have any ideas for making that more convenient specifically for unit tests? I guess it depends on what a user wants to accomplish in unit tests, though. In both of the tests in your example, I don't see that as a big problem, if disabling requests-cache is an acceptable solution. But if for some reason you wanted both request mocking and caching features at the same time (for example, in the unit tests for requests-cache itself), the nested contextmanagers in that example don't do that. Personally I don't think any changes are needed in requests-mock just for this case, but maybe for the more general case of working with other libraries that modify |
@jamielennox Fyi, I added some better examples here. The last one is probably the most potentially useful. Also see requests-cache/requests-cache#330 And let me know if you have any thoughts on that. Thanks! |
Hello and thank you for your work!
We're using requests-cache in our project to reduce load for redundant requests. When we started to use
requests_mock
we noticed weird behavior. The following minimal working example showcases that:We can observe that:
test_mock_cache
does not failtest_mock2_cache
andtest_mock3_cache
both fail on the line withcache=True
As a workaround we disable caching for our tests. But we still wanted to report this interaction.
The text was updated successfully, but these errors were encountered: