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")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…