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

Posted on

Question :

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

How do I test the following code with unittest.mock:

def testme(filepath):
    with open(filepath) as f:

Answer #1:

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:

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

>>> open_name = '' % __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')
Answered By: fuzzyman

Answer #2:

Python 3

Patch 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_open
with patch("", mock_open(read_data="data")) as mock_file:
    assert open("path/to/open").read() == "data"

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("", new_callable=mock_open, read_data="data")
def test_patch(mock_file):
    assert open("path/to/open").read() == "data"

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

Python 2

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

from mock import patch, mock_open
with patch("", mock_open(read_data="data")) as mock_file:
    assert open("path/to/open").read() == "data"
Answered By: Michele d’Amico

Answer #3:

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')
Answered By: David

Answer #4:

To use mock_open for a simple file read() (the original mock_open snippet already given on this page is geared more for write):

my_text = "some text to return when read() is called on the file object"
mocked_open_function = mock.mock_open(read_data=my_text)

with mock.patch("", mocked_open_function):
    with open("any_string") as f:

Note as per docs for mock_open, this is specifically for read(), so won’t work with common patterns like for line in f, for example.

Uses python 2.6.6 / mock 1.0.1

Answered By: jlb83

Answer #5:

The top answer is useful but I expanded on it a bit.

If you want to set the value of your file object (the f in as f) based on the arguments passed to open() here’s one way to do it:

def save_arg_return_data(*args, **kwargs):
    mm = MagicMock(spec=file)
    mm.__enter__.return_value = do_something_with_data(*args, **kwargs)
    return mm
m = MagicMock()
m.side_effect = save_arg_return_array_of_data

# if your open() call is in the file mymodule.animals 
# use mymodule.animals as name_of_called_file
open_name = '' % name_of_called_file

with patch(open_name, m, create=True):
    #do testing here

Basically, open() will return an object and with will call __enter__() on that object.

To mock properly, we must mock open() to return a mock object. That mock object should then mock the __enter__() call on it (MagicMock will do this for us) to return the mock data/file object we want (hence mm.__enter__.return_value). Doing this with 2 mocks the way above allows us to capture the arguments passed to open() and pass them to our do_something_with_data method.

I passed an entire mock file as a string to open() and my do_something_with_data looked like this:

def do_something_with_data(*args, **kwargs):
    return args[0].split("n")

This transforms the string into a list so you can do the following as you would with a normal file:

for line in file:
    #do action
Answered By: theannouncer

Answer #6:

I might be a bit late to the game, but this worked for me when calling open in another module without having to create a new file.

import unittest
from mock import Mock, patch, mock_open
from MyObj import MyObj

class TestObj(unittest.TestCase):
    open_ = mock_open()
    with patch.object(__builtin__, "open", open_):
        ref = MyObj()"myfile.txt")
    assert open_.call_args_list == [call("myfile.txt", "wb")]

class MyObj(object):
    def save(self, filename):
        with open(filename, "wb") as f:
            f.write("sample text")

By patching the open function inside the __builtin__ module to my mock_open(), I can mock writing to a file without creating one.

Note: If you are using a module that uses cython, or your program depends on cython in any way, you will need to import cython’s __builtin__ module by including import __builtin__ at the top of your file. You will not be able to mock the universal __builtin__ if you are using cython.

Answered By: Leo C Han

Answer #7:

To patch the built-in open() function with unittest:

This worked for a patch to read a json config.

class ObjectUnderTest:
    def __init__(self, filename: str):
        with open(filename, 'r') as f:
            dict_content = json.load(f)

The mocked object is the io.TextIOWrapper object returned by the open() function

        return_value=io.TextIOWrapper(io.BufferedReader(io.BytesIO(b'{"test_key": "test_value"}'))))
    def test_object_function_under_test(self, mocker):
Answered By: pabloberm

Answer #8:

If you don’t need any file further, you can decorate the test method:

@patch('', mock_open(read_data="data"))
def test_testme():
    result = testeme()
    assert result == "data"
Answered By: Ferdinando de Melo

Leave a Reply

Your email address will not be published. Required fields are marked *