<div dir="ltr"><br clear="all"><div><div>I've the following code in the <b>__init__.py</b> file</div><div><br></div><div> <b>from time import sleep</b></div><div><b> from functools import wraps</b></div><div><b><br></b></div><div><b> def multi_try(func):</b></div><div><b> @wraps(func)</b></div><div><b> def inner(*args, **kwargs):</b></div><div><b> count = 0</b></div><div><b> while count < 5:</b></div><div><b> resp = func(*args, **kwargs)</b></div><div><b> if resp.status_code in [200, 201]:</b></div><div><b> return resp</b></div><div><b> sleep(1)</b></div><div><b> count += 1</b></div><div><b> return inner</b></div><div><br></div><div><br></div><div>While writing tests for the above decorator I'm not able to patch the the *time.sleep* properly. </div><div><br></div><div>See the test below, even though I've patched time module, still the sleep function inside the decorator getting called, thereby test case require 5+ seconds to finish.</div><div><br></div><div><br></div><div> <b>def test_multi_try_time():</b></div><div><b> with patch("time.sleep") as tm: </b></div><div><b> mocker = MagicMock(name="mocker")</b></div><div><b> mocker.__name__ = "blah"</b></div><div><b> resp_mock = MagicMock()</b></div><div><b> resp_mock.status_code=400</b></div><div><b> _json = '{"test":"twist"}'</b></div><div><b> resp_mock.json=_json</b></div><div><b> mocker.return_value = resp_mock</b></div><div><b> wrapped = multi_try(mocker)</b></div><div><b> resp = wrapped("p", "q")</b></div><div><b> assert mocker.call_count == 5</b></div><div><b> mocker.assert_called_with('p', 'q')</b></div><div><b> assert resp == None</b></div><div><br></div><div>Also I tried this,</div><div><br></div><div><b>`with patch("dir.__init__.time" ) as tm:</b>` </div><div><br></div><div>and </div><div><br></div><div><br></div><div>`<b>with patch("dir.utils.time" ) as tm:</b>`</div><div><br></div><div>That resulted in </div><div><br></div><div>`<b>AttributeError: <module 'dir/__init__.pyc'> does not have the attribute 'time'</b>`</div></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><font size="2" color="#3d85c6" face="garamond, serif"><i><b style="background-color:rgb(255,255,255)">Regards</b></i></font><div><font size="2" color="#3d85c6" face="garamond, serif"><i><b style="background-color:rgb(255,255,255)">Sijo Jose</b></i></font></div></div></div>
</div>