如何模拟with语句中使用的打开(使用Python中的Mock框架)?

问题:如何模拟with语句中使用的打开(使用Python中的Mock框架)?

如何使用模拟测试以下代码(使用模拟Michael Foord的Mock框架提供的补丁装饰器和哨兵):

def testme(filepath):
    with open(filepath, 'r') as f:
        return f.read()

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

def testme(filepath):
    with open(filepath) as f:
        return f.read()

回答 0

模拟0.7.0中的更改方式已更改,该模拟最终支持模拟python协议方法(魔术方法),尤其是使用MagicMock:

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

作为上下文管理器打开模拟的示例(来自模拟文档中的示例页面):

>>> 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')

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')

回答 1

mock_openmock框架的一部分,使用非常简单。patch用作上下文返回用于替换已修补对象的对象:您可以使用它使测试更简单。

Python 3.x

使用builtins代替__builtin__

from unittest.mock import patch, mock_open
with 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")

Python 2.7

mock不属于unittest您,您应该修补__builtin__

from mock import patch, mock_open
with 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")

装饰箱

如果将的结果用作的patch修饰符用作装饰器mock_open(),则new patch可能会有些奇怪。

在这种情况下,最好使用new_callable patch的参数并要记住,每个patch不使用的额外参数都会按照文档中的new_callable描述传递给函数。patch

patch()使用任意关键字参数。这些将在构造时传递给Mock(或new_callable)。

例如,Python 3.x的修饰版本为:

@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")

请记住,在这种情况下,patch会将模拟对象添加为测试函数的参数。

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_open
with 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_open
with 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")

回答 2

使用最新版本的模拟,您可以使用真正有用的模拟_打开帮助器:

mock_open(模拟=无,读取数据=无)

一个辅助函数创建一个模拟来代替open的使用。它适用于直接调用或用作上下文管理器的open。

模拟参数是要配置的模拟对象。如果没有(默认),则将为您创建一个MagicMock,其API限于标准文件句柄上可用的方法或属性。

read_data是用于返回文件句柄的read方法的字符串。默认情况下,这是一个空字符串。

>>> 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')

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')

回答 3

要对一个简单文件使用嘲笑read()(打开)(此页面上已经给出的原始嘲笑的片段更适合写入):

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("__builtin__.open", mocked_open_function):
    with open("any_string") as f:
        print f.read()

请注意,根据嘲笑文档的doc,这是专门针对的read(),因此不适for line in f用于像这样的常见模式。

使用python 2.6.6 /模拟1.0.1

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("__builtin__.open", mocked_open_function):
    with open("any_string") as f:
        print f.read()

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


回答 4

最佳答案很有用,但我对此进行了扩展。

如果要基于传递给此处的参数设置文件对象(fin as f)的值,open()这是一种实现方法:

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 = '%s.open' % name_of_called_file

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

基本上,open()将返回一个对象并with调用__enter__()该对象。

为了正确模拟,我们必须模拟open()返回一个模拟对象。然后,该模拟对象应该模拟对其的__enter__()调用(MagicMock将为我们执行此操作),以返回我们想要的模拟数据/文件对象(因此mm.__enter__.return_value)。通过上面的方法使用2个模拟进行此操作,我们可以捕获传递给参数的参数open()并将其传递给我们的do_something_with_data方法。

我将整个模拟文件作为字符串传递给了open()我,do_something_with_data如下所示:

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

这会将字符串转换为列表,因此您可以像处理普通文件一样执行以下操作:

for line in file:
    #do action

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 = '%s.open' % 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

回答 5

我可能对游戏有些迟了,但是当我调用open另一个模块而不必创建新文件时,这对我有用。

test.py

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()
        ref.save("myfile.txt")
    assert open_.call_args_list == [call("myfile.txt", "wb")]

MyObj.py

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

通过open__builtin__模块内部的功能修补到my mock_open(),可以模拟写入文件而无需创建文件。

注意:如果您正在使用使用cython的模块,或者您的程序以任何方式依赖cython,则需要通过在文件顶部包含来导入cython的__builtin__模块import __builtin____builtin__如果您使用cython,则将无法模拟通用。

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.

test.py

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()
        ref.save("myfile.txt")
    assert open_.call_args_list == [call("myfile.txt", "wb")]

MyObj.py

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.


回答 6

要使用unittest修补内置的open()函数:

这适用于读取json配置的补丁。

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

模拟对象是open()函数返回的io.TextIOWrapper对象。

@patch("<src.where.object.is.used>.open",
        return_value=io.TextIOWrapper(io.BufferedReader(io.BytesIO(b'{"test_key": "test_value"}'))))
    def test_object_function_under_test(self, mocker):

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

@patch("<src.where.object.is.used>.open",
        return_value=io.TextIOWrapper(io.BufferedReader(io.BytesIO(b'{"test_key": "test_value"}'))))
    def test_object_function_under_test(self, mocker):

回答 7

如果您不再需要任何文件,则可以装饰测试方法:

@patch('builtins.open', mock_open(read_data="data"))
def test_testme():
    result = testeme()
    assert result == "data"

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

@patch('builtins.open', mock_open(read_data="data"))
def test_testme():
    result = testeme()
    assert result == "data"