Python 测试:使用模拟文件和 io.StringIO

Python testing: using a fake file with mock & io.StringIO

我正在尝试测试一些对文件进行操作的代码,但我似乎无法理解如何将真实文件替换为 mockio.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

我有一个 class 可以获取列表或文件,如果它是一个文件,它会打开它并将内容提取到列表中,然后对结果列表执行它需要执行的操作.

我有一个工作测试如下:

def test_CheckConfig_with_file():
    config = 'config.txt'
    expected = parsed_file_data
    actual = CheckConfig(config).config
    assert expected == actual

我想替换对文件系统的调用。我尝试用 io.StringIO 替换文件,但我从 os.path.isfile() 得到了 TypeError,因为它需要一个字符串、字节或整数。我也试过像这样模拟 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

但我仍然得到相同的 TypeError,因为 _io.StringIO 类型在 isfile 有机会 return 之前导致异常。

如何让 os.path.isfile 变为 return True,当我传递一个假文件时?或者这是我应该更改代码的建议?

只需模拟 os.path.isfile 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

这导致 if isinstance(data, list): 测试为假(因为 data 是一个字符串),然后 elif os.path.isfile(data): 返回 Trueopen(data) 调用以使用 mock_open() 结果中的模拟数据。

您可以使用 mock_open 变量断言 open() 是使用正确的数据调用的(例如 mock_open. assert_called_once_with('mocked/filename'))。

演示:

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

如果您想知道如何使用 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'

已找到此代码示例(但必须进行调整)here