Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created October 5, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/c963933fa3e44c4156eb to your computer and use it in GitHub Desktop.
Save anonymous/c963933fa3e44c4156eb to your computer and use it in GitHub Desktop.
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