Skip to content

Instantly share code, notes, and snippets.

@arthuralvim
Created February 23, 2016 06:58
Show Gist options
  • Save arthuralvim/a5be9f758cd8dc2f0d97 to your computer and use it in GitHub Desktop.
Save arthuralvim/a5be9f758cd8dc2f0d97 to your computer and use it in GitHub Desktop.
Studying mock objects in Python.
# -*- coding: utf-8 -*-
class NoConnectionException(Exception):
def __init__(self, message=None):
super(NoConnectionException, self).__init__(message)
self.message = 'No connection!'
class Human(object):
have_internet = False
def __init__(self, first_name, last_name, country='-', *args, **kwargs):
self.first_name = first_name
self.last_name = last_name
self.country = country
def get_connection(self):
if self.have_internet:
return True
else:
raise NoConnectionException
def connect_to_internet(self):
try:
self.get_connection()
self.go_to_hack_hands()
except Exception, e:
raise e
def go_to_hack_hands(self, echo=False):
self.message = '{0} {1} connecting to www.hackhands.com from {2}' \
.format(self.first_name, self.last_name, self.country)
if echo:
print self.message
def so_awesome_thing(self):
self.go_to_hack_hands()
print 'awesome things takes tooooooo long to run!'
self.awesome_thing = 10
if __name__ == '__main__':
hum = Human('Fernando', 'Rocha', 'Brazil')
hum.have_internet = True
hum.connect_to_internet()
hum = Human('Onaka', 'Mutu', 'Angola', False)
hum.have_internet = True
hum.connect_to_internet()
# -*- coding: utf-8 -*-
import mock
import unittest
from human import Human
@mock.patch.object(Human, 'have_internet', True)
class TestHuman(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(TestHuman, cls).setUpClass()
# cls.human = Human('Fernando', 'Rocha', 'Brazil')
# cls.human.connect_to_internet()
# cls.awesome_thing = cls.human.so_awesome_thing()
def setUp(self):
super(TestHuman, self).setUp()
self.not_awesome_var = 100
# self.human = Human('Fernando', 'Rocha', 'Brazil')
# self.human.connect_to_internet()
# self.awesome_thing = self.human.so_awesome_thing()
@classmethod
def tearDownClass(cls):
super(TestHuman, cls).tearDownClass()
def tearDown(self):
super(TestHuman, self).tearDown()
def test_acess_to_hack_hands(self):
self.human.go_to_hack_hands()
self.assertEqual(self.human.message, ('Fernando Rocha connecting to '
'www.hackhands.com from Brazil'))
def test_connection(self):
self.human.connect_to_internet()
self.assertEqual(self.human.message, ('Fernando Rocha connecting to '
'www.hackhands.com from Brazil'))
def test_not_awesome_var(self):
self.assertEqual(self.not_awesome_var, 100)
def test_awesome_thing_0(self):
self.assertEqual(self.human.awesome_thing, 10)
def test_awesome_thing_1(self):
self.assertEqual(self.human.awesome_thing, 10)
def test_awesome_thing_2(self):
self.assertEqual(self.human.awesome_thing, 10)
def test_awesome_thing_3(self):
self.assertEqual(self.human.awesome_thing, 10)
# more tests about awesome_thing ...
if __name__ == '__main__' and __package__ is None:
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment