[TIP] Mocking import functions

David Stanek dstanek at dstanek.com
Sat Feb 8 10:24:39 PST 2014


I cloned your gist, simplified the code and added the missing module to
come up with a working example of my suggestion.  Since gists don't yet
support directories I put my diff on dpaste[1].  You should be able to
apply the diff to a clone of your patch to see that my new test runs just
fine.  I left the broken test in the diff so that you can see a side by
side view.

My test run:

$ nosetests test_twitter.py
E.
======================================================================
ERROR: test_twitter.test_i_expect_this_to_fail__patching_wrong_thing
----------------------------------------------------------------------
Traceback (most recent call last):
... [ snip ] ...
Exception: the test should never see this

----------------------------------------------------------------------
Ran 2 tests in 0.005s

FAILED (errors=1)


1. http://dpaste.com/1593212/


On Fri, Feb 7, 2014 at 4:02 PM, Mahdi Yusuf <yusuf.mahdi at gmail.com> wrote:

> Bruno,
>
> I would like to avoid changing code, especially without a reason. I have
> already tried this route and doesn't work (tried it again now, for human
> error). :) mechanize_url still runs as implemented.
>
> Thanks for your response.
>
> --
> Mahdi Yusuf
>
>
> On Fri, Feb 7, 2014 at 3:43 PM, Bruno Oliveira <nicoddemus at gmail.com>wrote:
>
>> Hi Mahdi,
>>
>> If you can change your code, try to import the module instead of the
>> function in twitter_profile.py:
>>
>> from miners import task_utils
>>
>> And use it as such in the code.
>>
>>       try:
>>
>>
>>
>>
>>           html = task_utils.mechanize_url(url)
>>
>>
>>
>>
>>       except:
>>
>>
>>
>>
>>           ...
>>
>>
>> Then mocking using mock.patch('miners.tasks_utils.mechanize_url') should work.
>>
>>
>>
>>
>>
>>
>> Hope this helps,
>>
>>
>>
>> On Fri, Feb 7, 2014 at 2:18 PM, Mahdi Yusuf <yusuf.mahdi at gmail.com>wrote:
>>
>>> David,
>>>
>>> Thanks for your response. I tried that when I was exhausting all my
>>> options. That also doesn't work.
>>>
>>> --
>>> Mahdi Yusuf
>>>
>>>
>>> On Fri, Feb 7, 2014 at 11:07 AM, David Stanek <dstanek at dstanek.com>wrote:
>>>
>>>> Instead of mock.patch('miners.tasks_utils.mechanize_url') you should
>>>> try mock.patch('twitter_profile.mechanize_url').
>>>>
>>>> Once your twitter_profile module is imported it will have a reference
>>>> to mechanize_url.  When you patch miners.tasks_utils you are not changing
>>>> this reference.
>>>>
>>>>
>>>>  On Fri, Feb 7, 2014 at 10:42 AM, Mahdi Yusuf <yusuf.mahdi at gmail.com>wrote:
>>>>
>>>>>  I am at my wits end pretty much. I am not sure what is happening
>>>>> here.
>>>>>
>>>>> I am unable to mock an import function. My code is here<https://gist.github.com/myusuf3/685083e70bf3f93fd43c>.
>>>>> After some reading I know you patch where the function is used not where
>>>>> its defined.
>>>>>
>>>>> I have also tried everything else but the mechanize_url is till
>>>>> running as implemented in code not mocked in test.
>>>>>
>>>>> Thoughts?
>>>>>
>>>>> --
>>>>> Mahdi Yusuf
>>>>>
>>>>> _______________________________________________
>>>>> testing-in-python mailing list
>>>>> testing-in-python at lists.idyll.org
>>>>> http://lists.idyll.org/listinfo/testing-in-python
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> David
>>>> blog: http://www.traceback.org
>>>> twitter: http://twitter.com/dstanek
>>>> www: http://dstanek.com
>>>>
>>>
>>>
>>>
>>> --
>>> Mahdi Yusuf
>>>
>>> _______________________________________________
>>> testing-in-python mailing list
>>> testing-in-python at lists.idyll.org
>>> http://lists.idyll.org/listinfo/testing-in-python
>>>
>>>
>>
>
>
> --
> Mahdi Yusuf
>



-- 
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek
www: http://dstanek.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140208/d1ef9e31/attachment.html>


More information about the testing-in-python mailing list