[TIP] py.test + mock - how to test attribute "call"? REST api testing requests
Daniel Bradburn
moagstar at gmail.com
Thu Nov 3 02:35:55 PDT 2016
Hi Sylvain,
Ah I see what you are trying to do now, I think the problem is that your
function is returning r.text which is a new mock object when really you
want to assert attribute access on the original return value from requests,
this seems to reflect your use case:
>>> import mock
>>> r = mock.MagicMock()
>>> r = r.text
>>> assert 'text' in r._mock_parent._mock_children
However, it is probably unwise to access the protected attributes like
this, perhaps you could just mock requests.get(call_url,
headers=headers) yourself,
I think you can probably use something like this as a starting point for
what you are trying to do...
import requests
import mock
def function_under_test(url, text):
r = requests.get(url)
if text:
return r.text
else:
return r.json()
r = mock.MagicMock()
with mock.patch('requests.get', return_value=r) as get:
result = function_under_test('https://www.google.nl', False)
str(result)
r.json.assert_called_once()
r.text.__str__.assert_not_called()
r = mock.MagicMock()
with mock.patch('requests.get', return_value=r) as get:
result = function_under_test('https://www.google.nl', True)
r.json.assert_not_called()
str(result)
r.text.__str__.assert_called_once()
Hope this helps.
Regards
Dan
2016-11-03 7:58 GMT+01:00 Sylvain Viart <sylvain at opensource-expert.com>:
> Hi Dan,
>
> Le 02/11/2016 à 19:10, Daniel Bradburn a écrit :
> >
> > Hi Sylvain, perhaps the responses library might be of use to you for
> mocking the requests library
> >
> > https://pypi.python.org/pypi/responses
>
> Thanks Dan.
>
> I also found it here, with other tools, I share the link:
> http://stackoverflow.com/questions/15753390/python-
> mock-requests-and-the-response
>
> Coming back to my original question, using pytest-mock.
>
> It will create a MagickMock object which grabs all call, and even as a
> returned object.
>
> My code give the following steps:
> https://github.com/opensource-expert/powerdns-shell-wrapper/
> blob/dev-override-cmd-line/pdns_zone.py#L81
>
> r = requests.get(call_url, headers=headers)
> […]
> return r.text
>
> And my test call it that way:
>
> r = p.exec_pdns_api('GET', rest_url)
>
> So r becomes a returned value of a requests call, the MagickMock object
> is reflecting that:
>
> >> how to I make an assertion that the r.text has been called/returned?
> >> r is <MagicMock name='get().text' id='139703066450704'>
>
> What assertion can I use on such object in fact?
>
> Regards,
> Sylvain.
> --
> Sylvain Viart - DevOps système linux - freelance developer
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161103/098f572b/attachment.html>
More information about the testing-in-python
mailing list