[TIP] py.test + mock - how to test attribute "call"? REST api testing requests

Sylvain Viart sylvain at opensource-expert.com
Wed Nov 2 23:58:08 PDT 2016


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161103/40c03382/attachment.htm>


More information about the testing-in-python mailing list