[TIP] py.test + mock - how to test attribute "call"?

Sylvain Viart sylvain at opensource-expert.com
Mon Oct 31 01:28:35 PDT 2016


I writing a command line wrapper for managing powerdns via its
REST interface.

I'm now refactoring my proof of concept to a full python script. I'm
using requests library to communicate the Json code build by my python
class. I'm adding unittest to ensure no regression, from first bash + jq
+ python + json + jinja2 + yaml version.

I'm having trouble to understand how to use correctly mock object to
test correctness of requests' call and also returned object.

I'm using py.test + pytest-mock

I would like to test here that r.text is returned if I call:

r = p.exec_pdns_api('GET', rest_url, text=True)

how to I make an assertion that the r.text has been called/returned?

r is <MagicMock name='get().text' id='139703066450704'>

r.text.assert_called() is not the good assertion
test code here:

I may using a wrong way of testing it… Any hint would be appreciated.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161031/ca28c11a/attachment.html>

More information about the testing-in-python mailing list