[TIP] Not able to patch modules used in __init__.py
Sijo Jose
mailtosijojose at gmail.com
Thu Aug 25 06:55:56 PDT 2016
I've the following code in the *__init__.py* file
*from time import sleep*
* from functools import wraps*
* def multi_try(func):*
* @wraps(func)*
* def inner(*args, **kwargs):*
* count = 0*
* while count < 5:*
* resp = func(*args, **kwargs)*
* if resp.status_code in [200, 201]:*
* return resp*
* sleep(1)*
* count += 1*
* return inner*
While writing tests for the above decorator I'm not able to patch the the
*time.sleep* properly.
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.
*def test_multi_try_time():*
* with patch("time.sleep") as tm: *
* mocker = MagicMock(name="mocker")*
* mocker.__name__ = "blah"*
* resp_mock = MagicMock()*
* resp_mock.status_code=400*
* _json = '{"test":"twist"}'*
* resp_mock.json=_json*
* mocker.return_value = resp_mock*
* wrapped = multi_try(mocker)*
* resp = wrapped("p", "q")*
* assert mocker.call_count == 5*
* mocker.assert_called_with('p', 'q')*
* assert resp == None*
Also I tried this,
*`with patch("dir.__init__.time" ) as tm:*`
and
`*with patch("dir.utils.time" ) as tm:*`
That resulted in
`*AttributeError: <module 'dir/__init__.pyc'> does not have the attribute
'time'*`
--
*Regards*
*Sijo Jose*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20160825/1fb5cecd/attachment.html>
More information about the testing-in-python
mailing list