Skip to content

Instantly share code, notes, and snippets.

Created Oct 5, 2015
Embed
What would you like to do?
my_singleton = None
class Foo(object):
def __init__(self):
pass
def do(arg):
return arg
def process_foo(foo, arg):
foo.do(arg)
def do_something():
# Heavy API call
raise Exception()
return 1
def logic():
# something
api_response = do_something()
# something
return api_response
def print_singleton():
return my_singleton
######### testfile
from unittest import mock
import demo
from demo import my_singleton
foo_mock = mock.Mock()
demo.process_foo(foo_mock, 4)
foo_mock.do.assert_called_with(4)
@mock.patch('demo.do_something')
def test_logic(m):
m.return_value = 1
result = demo.logic()
m.assert_called_with()
assert result == 1
test_logic()
demo.my_singleton = 5
my_singleton = 6
assert demo.print_singleton() == 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment