How do I mock an open used in a with statement (using the Mock framework in Python)? How do I mock an open used in a with statement (using the Mock framework in Python)? python python

How do I mock an open used in a with statement (using the Mock framework in Python)?


Python 3

Patch builtins.open and use mock_open, which is part of the mock framework. patch used as a context manager returns the object used to replace the patched one:

from unittest.mock import patch, mock_openwith patch("builtins.open", mock_open(read_data="data")) as mock_file:    assert open("path/to/open").read() == "data"    mock_file.assert_called_with("path/to/open")

If you want to use patch as a decorator, using mock_open()'s result as the new= argument to patch can be a little bit weird. Instead, use patch's new_callable= argument and remember that every extra argument that patch doesn't use will be passed to the new_callable function, as described in the patch documentation:

patch() takes arbitrary keyword arguments. These will be passed to the Mock (or new_callable) on construction.

@patch("builtins.open", new_callable=mock_open, read_data="data")def test_patch(mock_file):    assert open("path/to/open").read() == "data"    mock_file.assert_called_with("path/to/open")

Remember that in this case patch will pass the mocked object as an argument to your test function.

Python 2

You need to patch __builtin__.open instead of builtins.open and mock is not part of unittest, you need to pip install and import it separately:

from mock import patch, mock_openwith patch("__builtin__.open", mock_open(read_data="data")) as mock_file:    assert open("path/to/open").read() == "data"    mock_file.assert_called_with("path/to/open")


The way to do this has changed in mock 0.7.0 which finally supports mocking the python protocol methods (magic methods), particularly using the MagicMock:

http://www.voidspace.org.uk/python/mock/magicmock.html

An example of mocking open as a context manager (from the examples page in the mock documentation):

>>> open_name = '%s.open' % __name__>>> with patch(open_name, create=True) as mock_open:...     mock_open.return_value = MagicMock(spec=file)......     with open('/some/path', 'w') as f:...         f.write('something')...<mock.Mock object at 0x...>>>> file_handle = mock_open.return_value.__enter__.return_value>>> file_handle.write.assert_called_with('something')


With the latest versions of mock, you can use the really useful mock_open helper:

mock_open(mock=None, read_data=None)

A helper function to create a mock to replace the use of open. It works for open called directly or used as a context manager.

The mock argument is the mock object to configure. If None (the default) then a MagicMock will be created for you, with the API limited to methods or attributes available on standard file handles.

read_data is a string for the read method of the file handle to return. This is an empty string by default.

>>> from mock import mock_open, patch>>> m = mock_open()>>> with patch('{}.open'.format(__name__), m, create=True):...    with open('foo', 'w') as h:...        h.write('some stuff')>>> m.assert_called_once_with('foo', 'w')>>> handle = m()>>> handle.write.assert_called_once_with('some stuff')