Created
September 10, 2012 15:45
-
-
Save daragh/3691654 to your computer and use it in GitHub Desktop.
Example code for mocking a module in Python
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
''' | |
Example code to show how it is possible to mock an entire module by patching | |
the sys.modules dict using mock ( http://www.voidspace.org.uk/python/mock/ ). | |
''' | |
from mock import patch, MagicMock | |
def test_import_patching(): | |
module_mock = MagicMock() | |
with patch.dict('sys.modules', **{ | |
'unimportable_module': module_mock, | |
'unimportable_module.submodule': module_mock, | |
}): | |
import unimportable_module.submodule | |
assert unimportable_module.submodule == module_mock.submodule | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Take a look at this: https://github.com/posener/mock-import