<div dir="ltr">Hi Sylvain,<div><br></div><div>Ah I see what you are trying to do now, I think the problem is that your function is returning <span style="color:rgb(0,0,0);font-family:consolas">r.text</span> 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:<br></div><div><pre style="color:rgb(0,0,0);font-family:consolas">>>> <span style="color:rgb(0,0,128);font-weight:bold">import </span>mock<br>>>> r = mock.MagicMock()<br>>>> r = r.text<br>>>> <span style="color:rgb(0,0,128);font-weight:bold">assert </span><span style="color:rgb(0,128,0);font-weight:bold">'text' </span><span style="color:rgb(0,0,128);font-weight:bold">in </span>r._mock_parent._mock_children</pre></div><div>However, it is probably unwise to access the protected attributes like this, perhaps you could just mock <span style="color:rgb(0,0,0);font-family:consolas">requests.get(call_url, </span><span style="font-family:consolas;color:rgb(102,0,153)">headers</span><span style="color:rgb(0,0,0);font-family:consolas">=headers) </span>yourself, I think you can probably use something like this as a starting point for what you are trying to do...</div><div><br></div><div><pre style="color:rgb(0,0,0);font-family:consolas"><span style="color:rgb(0,0,128);font-weight:bold">import </span>requests<br><span style="color:rgb(0,0,128);font-weight:bold">import </span>mock<br><br><span style="color:rgb(0,0,128);font-weight:bold">def </span>function_under_test(url, text):<br> r = requests.get(url)<br> <span style="color:rgb(0,0,128);font-weight:bold">if </span>text:<br> <span style="color:rgb(0,0,128);font-weight:bold">return </span>r.text<br> <span style="color:rgb(0,0,128);font-weight:bold">else</span>:<br> <span style="color:rgb(0,0,128);font-weight:bold">return </span>r.json()<br><br>r = mock.MagicMock()<br><span style="color:rgb(0,0,128);font-weight:bold">with </span>mock.patch(<span style="color:rgb(0,128,0);font-weight:bold">'requests.get'</span>, <span style="color:rgb(102,0,153)">return_value</span>=r) <span style="color:rgb(0,0,128);font-weight:bold">as </span>get:<br> result = function_under_test(<span style="color:rgb(0,128,0);font-weight:bold">'<a href="https://www.google.nl">https://www.google.nl</a>'</span>, <span style="color:rgb(0,0,128)">False</span>)<br> <span style="color:rgb(0,0,128)">str</span>(result)<br> r.json.assert_called_once()<br> r.text.<span style="color:rgb(178,0,178)">__str__</span>.assert_not_called()<br><br>r = mock.MagicMock()<br><span style="color:rgb(0,0,128);font-weight:bold">with </span>mock.patch(<span style="color:rgb(0,128,0);font-weight:bold">'requests.get'</span>, <span style="color:rgb(102,0,153)">return_value</span>=r) <span style="color:rgb(0,0,128);font-weight:bold">as </span>get:<br> result = function_under_test(<span style="color:rgb(0,128,0);font-weight:bold">'<a href="https://www.google.nl">https://www.google.nl</a>'</span>, <span style="color:rgb(0,0,128)">True</span>)<br> r.json.assert_not_called()<br> <span style="color:rgb(0,0,128)">str</span>(result)<br> r.text.<span style="color:rgb(178,0,178)">__str__</span>.assert_called_once()</pre></div><div>Hope this helps.<br></div><div><br></div><div>Regards</div><div>Dan</div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-11-03 7:58 GMT+01:00 Sylvain Viart <span dir="ltr"><<a href="mailto:sylvain@opensource-expert.com" target="_blank">sylvain@opensource-expert.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
Hi Dan,
<br>
<p>
Le 02/11/2016 à 19:10, Daniel Bradburn a écrit :<br>
><br>
> Hi Sylvain, perhaps the responses library might be of use to
you for mocking the requests library<br>
><br>
> <a href="https://pypi.python.org/pypi/responses" title="https://pypi.python.org/pypi/responses" class="m_-5683306896591896811https" target="_blank">https://pypi.python.org/pypi/<wbr>responses</a>
</p>
<p>
Thanks Dan.
</p>
<p>
I also found it here, with other tools, I share the link:<br>
<a href="http://stackoverflow.com/questions/15753390/python-mock-requests-and-the-response" title="http://stackoverflow.com/questions/15753390/python-mock-requests-and-the-response" class="m_-5683306896591896811http" target="_blank">http://stackoverflow.com/<wbr>questions/15753390/python-<wbr>mock-requests-and-the-response</a>
</p>
<p>
Coming back to my original question, using pytest-mock.
</p>
<p>
It will create a MagickMock object which grabs all call, and even
as a returned object.
</p>
<p>
My code give the following steps: <br>
<a href="https://github.com/opensource-expert/powerdns-shell-wrapper/blob/dev-override-cmd-line/pdns_zone.py#L81" title="https://github.com/opensource-expert/powerdns-shell-wrapper/blob/dev-override-cmd-line/pdns_zone.py#L81" class="m_-5683306896591896811https" target="_blank">https://github.com/opensource-<wbr>expert/powerdns-shell-wrapper/<wbr>blob/dev-override-cmd-line/<wbr>pdns_zone.py#L81</a>
</p>
<p>
<tt>r = requests.get(call_url, headers=headers)</tt><br>
[…]<br>
<tt>return r.text</tt>
</p>
<p>
And my test call it that way:
</p>
<p>
<tt>r = p.exec_pdns_api('GET', rest_url)</tt>
</p>
<p>
So <tt>r </tt>becomes a returned value of a requests call, the
MagickMock object is reflecting that:
</p>
<p>
>> how to I make an assertion that the r.text has been
called/returned?<br>
>> r is <tt><MagicMock name='get().text'
id='139703066450704'></tt>
</p>
<p>
What assertion can I use on such object in fact?
</p>
<p>
Regards,<br>
Sylvain.<span class="HOEnZb"><font color="#888888"><br>
-- <br>
Sylvain Viart - DevOps système linux - freelance developer
</font></span></p>
</div>
<br>______________________________<wbr>_________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.<wbr>org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" rel="noreferrer" target="_blank">http://lists.idyll.org/<wbr>listinfo/testing-in-python</a><br>
<br></blockquote></div><br></div>