forked from trobert2/unittest_repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_first_mock.py
38 lines (26 loc) · 866 Bytes
/
test_first_mock.py
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
import unittest
import mock
import urllib2
import first
class FirstTest(unittest.TestCase):
def setUp(self):
#the method we are testing will be called from this object
self.obj = first.First()
def test_get_data(self):
path = 'http://example.com'
text = ' random text'
urllib2.Request = mock.MagicMock()
handle = mock.Mock()
urllib2.urlopen = mock.MagicMock(return_value=handle)
handle.read.return_value = text
############## the method we are testing ##########
response = self.obj.get_data(path)
###################################################
urllib2.urlopen.assert_called_with(urllib2.Request())
handle.read.assert_called_once_with()
self.assertEqual(response, text)
#def main():
# unittest.main()
#
#if __name__ == "__main__":
# main()