import unittest.mock as mock
mock_argparse = mock.Mock()
with mock.patch.dict('sys.modules', argparse=mock_argparse):
import argparse
print(argparse.ArgumentParser())
# <Mock name='mock.ArgumentParser()' id='140681471282448'>
As for mock_open patching:
m = mock_open()
with patch('__main__.open', m, create=True):
It seems like it creates __main__.open
attribute with mock object which shadows the built-in version as if you defined open()
function in your module. I suppose for the actual tests you should patch module_x.open()
where "module_x" is the module that actually calls open()
.