lundi 23 février 2015

Python unit tests with mock_open() and patch

I am having trouble patching the open method of my module with mock_open as suggested in the docs.



import my_module
import mock

def test_foo():
m = mock.mock_open()
with patch('my_module.open', m, create=True):
my_module.io_func()

IOError: [Errno 2] No such file or directory: 'foofile'


Any idea why the patch isn't working?


Alternatively, I can get the following the execute as expected:



import my_module
import mock

def test_foo():
m = mock.mock_open()
my_module.__builtins__['open'] = m
my_module.io_func()


However, this feels like a hack.


Aucun commentaire:

Enregistrer un commentaire