The patch argument must be a description of the location of the object, not the object itself. So your problem looks like you just need to argue for the patch argument.
Just for completeness though, here is a complete working example. Firstly, our test module:
# mod_a.py import urllib2 def myfunc(): opened_url = urllib2.urlopen() return opened_url.read()
Now configure our test:
# test.py from mock import patch, Mock import mod_a @patch('mod_a.urllib2.urlopen') def mytest(mock_urlopen): a = Mock() a.read.side_effect = ['resp1', 'resp2'] mock_urlopen.return_value = a res = mod_a.myfunc() print res assert res == 'resp1' res = mod_a.myfunc() print res assert res == 'resp2' mytest()
Running a test from the shell:
$ python test.py resp1 resp2
Edit : Oops, originally including the original error. (Checked how it was broken.) The code should be fixed.
source share