I wrote another mock object, this time replacing urlopen
from urllib2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| import urllib2
import StringIO
import unittest
class Dummy_urllib2(object):
def install(cls):
urllib2.urlopen = Dummy_urllib2.urlopen
install = classmethod(install)
def urlopen(self, url, data=None):
self.url = url
self.data = data
response = StringIO.StringIO("foo")
def geturl():
return url
response.geturl = geturl
def info():
return {}
response.info = info
return response
urlopen = classmethod(urlopen)
class TestDummy_urllib2(unittest.TestCase):
def test_install(self):
Dummy_urllib2.install()
url = 'http://notfound.example.org'
try:
r = urllib2.urlopen(url)
except urllib2.URLError, e:
self.fail("URLError raised, Dummy_urllib2 not installed or failed: %s" % e)
self.assertEqual(url, Dummy_urllib2.url)
self.assertEqual(url, r.geturl())
self.assertEqual(None, Dummy_urllib2.data)
self.assertEqual("foo", r.read())
if __name__ == '__main__':
unittest.main()
|
This time it comes with it’s own test suite. How meta!