^{}是^{}框架的一部分,使用起来非常简单。^{}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")
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')
实现这一点的方法在mock 0.7.0中已经改变,mock最终支持模拟python协议方法(magic方法),特别是使用MagicMock:
http://www.voidspace.org.uk/python/mock/magicmock.html
模拟作为上下文管理器打开的示例(来自模拟文档中的示例页):
^{} 是^{} 框架的一部分,使用起来非常简单。^{} used as context返回用于替换已修补对象的对象:您可以使用它简化测试。
Python3.x
使用
builtins
而不是__builtin__
。Python2.7
mock
不是unittest
的一部分,您应该修补__builtin__
装饰箱
如果使用
patch
作为装饰符,使用mock_open()
的结果作为new
patch
的参数可能有点奇怪。在这种情况下,最好使用} documentation 中所述。
new_callable
patch
的参数,并记住patch
不使用的每个额外参数都将传递给new_callable
函数,如^{例如,Python 3.x的修饰版本是:
记住,在这种情况下,
patch
将添加mock对象作为测试函数的参数。使用mock的最新版本,您可以使用真正有用的mock_open帮助程序:
相关问题 更多 >
编程相关推荐