mocking for get method in requests

George Fischhof george at fischhof.hu
Sat Mar 16 18:58:35 EDT 2019


Shakti Kumar <shakti.shrivastava13 at gmail.com> ezt írta (időpont: 2019. jan.
18., P, 18:18):

> Hello people,
> I noticed something weird (weird as per my current knowledge, though I know
> its subjective) today.
>
> sample.py file
>
> --
>
> import requests
> def random_testing():
>     out = requests.get('www.cisco.com')
>     a = out.json()
>     return a
>
>
> testing.py file
>
> --
>
> @patch(*’*sample.requests')
> def test_random_testing(self, mock_req):
>     mock_req.get('').return_value = 'Hello'
>     out = api.random_testing()
>
>
> Patching the sample.requests in this way does not lead the output of
> requests.get() function in sample.py file to be ‘Hello’ as indicated
> in
> mock_req.get('').return_value = 'Hello'
> Instead, it creates a new field called return_value in ‘out', and
> hence out.return_value is ‘Hello’ instead of just ‘out’.
>
> But if I patch it as,
>
> @patch(*’*sample.requests')
> def test_random_testing(self, mock_req):
>     mock_req.get.return_value = 'Hello'
>     out = api.random_testing()
>
> It does give the value of ‘out’ as ‘Hello’ in sample.py file.
> I know I am missing something, which is where I need some help :)
>
> Thanks.
>
> --
>
>
> <Shakti Kumar>
> UG, CSE,
> RVCE, Bengaluru.
> --
> https://mail.python.org/mailman/listinfo/python-list



Hi  Kumar,

I saw that there was no answer.
Perhaps you should check PyTest
https://docs.pytest.org/en/latest/contents.html
it has several plugins, it can mock, and can do much things.

BR,
George



More information about the Python-list mailing list