如何用unittest.mock测试下面的代码:
def testme(filepath):
with open(filepath) as f:
return f.read()
如何用unittest.mock测试下面的代码:
def testme(filepath):
with open(filepath) as f:
return f.read()
当前回答
这样做的方式在mock 0.7.0中已经改变,它最终支持模拟python协议方法(magic方法),特别是使用MagicMock:
http://www.voidspace.org.uk/python/mock/magicmock.html
一个作为上下文管理器打开的mock示例(来自mock文档中的示例页面):
>>> 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')
其他回答
在mock的最新版本中,你可以使用真正有用的mock_open helper:
mock_open(模拟= None, read_data =没有) 对象的帮助函数 用Mock代替open。它适用于open call direct或 用作上下文管理器。 mock参数是要配置的模拟对象。若无( 默认),然后将为您创建一个MagicMock,使用API 仅限于标准文件句柄上可用的方法或属性。 Read_data是文件句柄的读取方法的字符串 回报。默认情况下,这是一个空字符串。
>>> 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')
如果你不需要进一步的文件,你可以装饰测试方法:
@patch('builtins.open', mock_open(read_data="data"))
def test_testme():
result = testeme()
assert result == "data"
Python 3
补丁内置命令。打开并使用mock_open,它是模拟框架的一部分。用作上下文管理器的Patch返回用于替换补丁的对象:
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")
如果您想使用patch作为装饰器,那么使用mock_open()的结果作为patch的new=参数可能会有点奇怪。相反,使用patch的new_callable=参数,并记住patch不使用的每个额外参数将被传递给new_callable函数,如补丁文档中所述:
Patch()接受任意关键字参数。这些将在构造时传递给Mock(或new_callable)。
@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 2
你需要修补__builtin__。打开而不是内置。Open和mock不是unittest的一部分,你需要分别安装和导入它:
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")
使用assert简单的@patch
如果你想使用@patch。open()在处理程序内部被调用并被读取。
@patch("builtins.open", new_callable=mock_open, read_data="data")
def test_lambda_handler(self, mock_open_file):
lambda_handler(event, {})
这样做的方式在mock 0.7.0中已经改变,它最终支持模拟python协议方法(magic方法),特别是使用MagicMock:
http://www.voidspace.org.uk/python/mock/magicmock.html
一个作为上下文管理器打开的mock示例(来自mock文档中的示例页面):
>>> 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')