[TIP] Mocking import functions

Mateusz Łoskot mateusz at loskot.net
Fri Feb 7 13:09:47 PST 2014

On 7 February 2014 16:07, 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').

AFAIU, the following can be used as an alternative, can't it?

with mock.patch.dict('sys.modules', {'miners': mock.Mock()}):
    miners.tasks_utils.mechanize_url.return_value =  content_file.read()

Any major differences between the two approaches?

Best regards,
Mateusz  Łoskot, http://mateusz.loskot.net

More information about the testing-in-python mailing list