Skip to content

Instantly share code, notes, and snippets.

@jmemich
Created June 17, 2015 19:16
Show Gist options
  • Save jmemich/bebe8c958504e1390501 to your computer and use it in GitHub Desktop.
Save jmemich/bebe8c958504e1390501 to your computer and use it in GitHub Desktop.
mock test example
# https://docs.python.org/3/library/unittest.mock.html
from unittest.mock import MagicMock, Mock
from nose.tools import assert_equal, raises, assert_true, assert_raises
class A():
def __init__(self):
self.item = 'item'
def return_item(self, thing = None):
if not thing:
return(self.item)
else:
return(thing)
class TestA():
def __init__(self):
pass
def test_return_item(self):
# testing `MagicMock` for methods
t = A()
t.return_item = MagicMock(return_value = 3)
assert_equal(t.return_item('item'),3) ## TRUE
assert_equal(t.return_item(),3) ## TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment