如何模拟在with语句中使用的open(在Python中使用模拟框架)?

2024-04-20 11:01:23 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用mock测试以下代码(使用mock、patch decorator和Michael Foord's Mock framework提供的sentinel):

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

Tags: 代码defaswithdecoratorframeworkopenmock
3条回答

实现这一点的方法在mock 0.7.0中已经改变,mock最终支持模拟python协议方法(magic方法),特别是使用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')

^{}^{}框架的一部分,使用起来非常简单。^{}used as context返回用于替换已修补对象的对象:您可以使用它简化测试。

Python3.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")

Python2.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()的结果作为newpatch的参数可能有点奇怪。

在这种情况下,最好使用new_callablepatch的参数,并记住patch不使用的每个额外参数都将传递给new_callable函数,如^{} documentation中所述。

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

例如,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将添加mock对象作为测试函数的参数。

使用mock的最新版本,您可以使用真正有用的mock_open帮助程序:

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

相关问题 更多 >