Python测试:将假文件与模拟&io.StringIO一起使用

bor*_*cle 4 python unit-testing mocking python-mock

我正在尝试测试可在文件上运行的某些代码,但似乎无法理解如何使用替换实际文件,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
Run Code Online (Sandbox Code Playgroud)

我有一个可以接受列表或文件的类,如果它是一个文件,则将其打开并将其提取到列表中,然后对结果列表执行所需的操作。

我的工作测试如下:

def test_CheckConfig_with_file():
    config = 'config.txt'
    expected = parsed_file_data
    actual = CheckConfig(config).config
    assert expected == actual
Run Code Online (Sandbox Code Playgroud)

我想替换对文件系统的调用。我试过用替换文件,io.StringIO但从中得到一个TypeErroros.path.isfile()因为它期望是字符串,字节或整数。我也尝试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
Run Code Online (Sandbox Code Playgroud)

但是我仍然得到与导致异常TypeError_io.StringIO类型相同isfile的机会,然后才有机会返回一些东西。

os.path.isfile将假文件传递给True时,如何返回True?还是这是我应该更改代码的建议?

Mar*_*ers 6

只是模拟出两个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
Run Code Online (Sandbox Code Playgroud)

这将导致if isinstance(data, list):测试为假(因为它data是一个字符串),然后是elif os.path.isfile(data):return True,然后是open(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)]
Run Code Online (Sandbox Code Playgroud)

  • 非常好,谢谢!这正是我所需要的,并且帮助我更好地掌握了嘲笑。只是出于兴趣,您使用上下文管理器而不是“@mock.patch”装饰器是否有原因。在之前的测试中,我一直使用装饰器,但在这里我找不到方法,因为我使用“pytest.fixture”作为“open”调用的内容,直到在测试函数内部才可用。 (2认同)
  • 对于绊倒这个答案的任何人。mock_open当前不支持可迭代,因此如果您使用yield代替f.readlines(),则必须解决迭代问题。/sf/ask/1734592541/ (2认同)