Last active
December 11, 2015 08:09
-
-
Save alonho/4571551 to your computer and use it in GitHub Desktop.
mock globals and imports
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
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