Skip to content

Instantly share code, notes, and snippets.

@alonho
Last active December 11, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonho/4571551 to your computer and use it in GitHub Desktop.
Save alonho/4571551 to your computer and use it in GitHub Desktop.
mock globals and imports
from foo import SomeClass
import mock
a = 100
def global_variable():
with mock.patch(__name__ + '.a'):
print a
def class_patch():
# read about 'where to patch' to see why not patch 'foo.SomeClass'
# http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch
with mock.patch(__name__ + '.SomeClass'):
print SomeClass
if __name__ == '__main__':
global_variable()
class_patch()
# prints:
# <MagicMock name='a' id='4311171920'>
# <MagicMock name='SomeClass' id='4311195984'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment