mocking for get method in requests
Shakti Kumar
shakti.shrivastava13 at gmail.com
Fri Jan 18 12:16:59 EST 2019
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.
More information about the Python-list
mailing list