<div dir="ltr"><br clear="all"><div><div>I&#39;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 &lt; 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&#39;m not able to patch the the *time.sleep* properly.  </div><div><br></div><div>See the test below, even though I&#39;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(&quot;time.sleep&quot;) as tm: </b></div><div><b>           mocker = MagicMock(name=&quot;mocker&quot;)</b></div><div><b>           mocker.__name__ = &quot;blah&quot;</b></div><div><b>           resp_mock = MagicMock()</b></div><div><b>           resp_mock.status_code=400</b></div><div><b>           _json = &#39;{&quot;test&quot;:&quot;twist&quot;}&#39;</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(&quot;p&quot;, &quot;q&quot;)</b></div><div><b>           assert mocker.call_count == 5</b></div><div><b>           mocker.assert_called_with(&#39;p&#39;, &#39;q&#39;)</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(&quot;dir.__init__.time&quot; ) as tm:</b>` </div><div><br></div><div>and </div><div><br></div><div><br></div><div>`<b>with patch(&quot;dir.utils.time&quot; ) as tm:</b>`</div><div><br></div><div>That resulted in </div><div><br></div><div>`<b>AttributeError: &lt;module &#39;dir/__init__.pyc&#39;&gt; does not have the attribute &#39;time&#39;</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>