Skip to content

Instantly share code, notes, and snippets.

@robinsonkwame
Created December 12, 2014 00:44
Show Gist options
  • Save robinsonkwame/2c157f90c924209a76e6 to your computer and use it in GitHub Desktop.
Save robinsonkwame/2c157f90c924209a76e6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import urllib
class Worker(object):
def __init__(self, myurl):
self.myurl = myurl
def dowork(self):
resp = urllib.request.urlopen(self.myurl)
print(resp.read())
#!/usr/bin/env python
import nose, unittest
from unittest import mock
from unittest.mock import patch
from worker import Worker
class WorkerTest(unittest.TestCase):
def setUp(self):
self.wrkr = Worker('http://www.234234ljk.com')
return
@patch('worker.urllib.request.urlopen', auto_spec=True)
def dowork_test(self, urlopen):
myobj = mock.Mock()
myobj.read.return_value = "This was successfully mocked."
urlopen.return_value = myobj
self.wrkr.dowork()
assert True == myobj.read.called
if '__main__' == __name__:
nose.main() # nose is so cool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment