[英]Python testing: using a fake file with mock & io.StringIO
I'm trying to test some code that operates on a file, and I can't seem to get my head around how to replace using a real file with mock
and io.StringIO
My code is pretty much the following:我正在尝试测试一些对文件进行操作的代码,但我似乎无法理解如何使用带有
mock
和io.StringIO
的真实文件io.StringIO
我的代码几乎如下:
class CheckConfig(object):
def __init__(self, config):
self.config = self._check_input_data(config)
def _check_input_data(self, data):
if isinstance(data, list):
return self._parse(data)
elif os.path.isfile(data):
with open(data) as f:
return self._parse(f.readlines())
def _parse(self, data):
return data
I have a class that can take either a list or a file, if it's a file it opens it and extracts the contents into a list, and then does what it needs to do to the resulting list.我有一个可以接受列表或文件的类,如果它是一个文件,它会打开它并将内容提取到一个列表中,然后对结果列表执行它需要执行的操作。
I have a working test as follows:我有一个工作测试如下:
def test_CheckConfig_with_file():
config = 'config.txt'
expected = parsed_file_data
actual = CheckConfig(config).config
assert expected == actual
I want to replace the call to the filesystem.我想替换对文件系统的调用。 I have tried replacing the file with
io.StringIO
but I get a TypeError
from os.path.isfile()
as it's expecting either a string, bytes or int.我尝试用
io.StringIO
替换文件,但我从os.path.isfile()
得到一个TypeError
,因为它需要一个字符串、字节或整数。 I also tried mocking the isfile
method like so:我也尝试像这样
isfile
方法:
@mock.patch('mymodule.os.path')
def test_CheckConfig_with_file(mock_path):
mock_path.isfile.return_value = True
config = io.StringIO('data')
expected = parsed_file_data
actual = CheckConfig(config).config
assert expected == actual
but I still get the same TypeError
as the _io.StringIO
type is causing the exception before isfile
gets a chance to return something.但我仍然得到相同的
TypeError
因为_io.StringIO
类型在isfile
有机会返回某些东西之前导致异常。
How can I get os.path.isfile
to return True, when I pass it a fake file?当我传递一个假文件时,如何让
os.path.isfile
返回 True? Or is this a suggestion I should change my code?或者这是我应该更改代码的建议?
Just mock out both os.path.isfile
and the open()
call, and pass in a fake filename (you are not expected to pass in an open file, after all).只需模拟
os.path.isfile
和open()
调用,并传入一个假文件名(毕竟你不应该传入一个打开的文件)。
The mock library includes a utility for the latter: mock_open()
:模拟库包括后者的实用程序:
mock_open()
:
@mock.patch('os.path.isfile')
def test_CheckConfig_with_file(mock_isfile):
mock_isfile.return_value = True
config_data = mock.mock_open(read_data='data')
with mock.patch('mymodule.open', config_data) as mock_open:
expected = parsed_file_data
actual = CheckConfig('mocked/filename').config
assert expected == actual
This causes the if isinstance(data, list):
test to be false (because data
is a string instead), followed by the elif os.path.isfile(data):
returning True
, and the open(data)
call to use your mocked data from the mock_open()
result.这会导致
if isinstance(data, list):
test 为 false(因为data
是一个字符串),然后是elif os.path.isfile(data):
返回True
和open(data)
调用以使用您的来自mock_open()
结果的mock_open()
数据。
You can use the mock_open
variable to assert that open()
was called with the right data ( mock_open. assert_called_once_with('mocked/filename')
for example).您可以使用
mock_open
变量来断言open()
是用正确的数据调用的( mock_open. assert_called_once_with('mocked/filename')
)。
Demo:演示:
>>> import os.path
>>> from unittest import mock
>>> class CheckConfig(object):
... def __init__(self, config):
... self.config = self._check_input_data(config)
... def _check_input_data(self, data):
... if isinstance(data, list):
... return self._parse(data)
... elif os.path.isfile(data):
... with open(data) as f:
... return self._parse(f.readlines())
... def _parse(self, data):
... return data
...
>>> with mock.patch('os.path.isfile') as mock_isfile:
... mock_isfile.return_value = True
... config_data = mock.mock_open(read_data='line1\nline2\n')
... with mock.patch('__main__.open', config_data) as mock_open:
... actual = CheckConfig('mocked/filename').config
...
>>> actual
['line1\n', 'line2\n']
>>> mock_open.mock_calls
[call('mocked/filename'),
call().__enter__(),
call().readlines(),
call().__exit__(None, None, None)]
In case you end up here wondering how to solve this using the pytest-mock library, here is how you do it:如果你最终想知道如何使用pytest-mock库解决这个问题,你可以这样做:
def test_open(mocker):
m = mocker.patch('builtins.open', mocker.mock_open(read_data='bibble'))
with open('foo') as h:
result = h.read()
m.assert_called_once_with('foo')
assert result == 'bibble'
This code example was found (but had to be adjusted) here .此代码示例在此处找到(但必须进行调整)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.