-
-
Save anonymous/c963933fa3e44c4156eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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