Python 模拟内置“打开”在一个类中使用两个不同的文件

问题描述 投票:0回答:5

当两个文件都使用上下文管理器时,我无法弄清楚如何模拟类中打开的两个文件。我知道如何使用模拟模块对一个上下文管理的文件执行此操作,如下所示:

@patch('__builtin__.open')
def test_interface_mapping(self, mock_config):
        m = MagicMock(spec=file)
        handle = m.return_value.__enter__.return_value
        handle.__iter__.return_value = ('aa', 'bb')

我的问题是当一个类在同一个调用中打开两个不同的文件时如何执行此操作。就我而言,类

__init__()
将文件预加载到两个地图中。该类在其他类中使用。我想模拟这两个文件的加载以提供我的测试数据,以便可以根据我预加载的测试文件内容测试使用 IfAddrConfig 对象的其他类。

这是我正在努力处理的类的一个示例,它在

__init__()
中加载两个文件,我想模拟这两个文件来加载我的测试注入文件内容。 getInterfaceMap() 是经常调用的函数,所以我不希望每次调用都加载和解析文件,因此在
__init__()
中预加载地图一次。

class IfAddrConfig(object):
    def __init__(self):
        # Initialize the static maps once since they require file operations
        # that we do not want to be calling every time getInterfaceMap() is used
        self.settings_map = self.loadSettings()
        self.config_map = self.loadConfig()

    def loadConfig(self):
        config_map = defaultdict(dict)
        with open(os.path.join('some_path.cfg'), 'r') as stream:
            for line in stream:
                # Parse line and build up config_map entries
        return config_map

    def loadSettings(self):
        settings_map = {}
        with open('another_path.cfg', 'r') as stream:
            for line in stream:
                # Parse line and build up settings_map entries
        return settings_map

    def getInterfaceMap(self, interface):
        # Uses both the settings and config maps to finally create a composite map
        # that is returned to called
        interface_map = {}
        for values in self.config_map.values():
            # Accesss self.settings_map and combine/compare entries with
            # self.config_map values to build new composite mappings that
            # depend on supplied interface value
        return interface_map
python unit-testing mocking contextmanager
5个回答
26
投票

您必须使用已修补的

side_effect
对象 (
open
) 的
mock_open
属性,并且不要忘记为
return_value
方法设置
__exit__

@patch('__builtin__.open', spec=open)
def test_interface_mapping(self, mock_open):
    handle1 = MagicMock()
    handle1.__enter__.return_value.__iter__.return_value = ('aa', 'bb')
    handle1.__exit__.return_value=False
    handle2 = MagicMock()
    handle2.__enter__.return_value.__iter__.return_value = ('AA', 'BB')
    handle2.__exit__.return_value=False
    mock_open.side_effect = (handle1, handle2)
    with open("ppp") as f:
        self.assertListEqual(["aa","bb"],[x for x in f])
    with open("ppp") as f:
        self.assertListEqual(["AA","BB"],[x for x in f])

[编辑] 我找到了一种更优雅的方法来做到这一点在 contextlib 中使用时模拟内置“open”函数

所以你可以像这样重写测试

@patch('__builtin__.open', new_callable=mock_open, read_data="aa\nbb")
def test_interface_mapping_new(self, mo):
    handlers = (mo.return_value, mock_open(read_data="AA\nBB").return_value)
    mo.side_effect = handlers
    with open("ppp") as f:
        self.assertEqual("aa\nbb",f.read())
    with open("ppp") as f:
        self.assertEqual("AA\nBB",f.read())

从 python 3.4 开始,你还可以使用 readline()、readlines() 而无需模拟其他任何东西。


10
投票

如果您需要对文件内容进行更多控制,您可以使用包装函数。 它按照文件名替换文件的内容,就像原始

open
那样。

import unittest.mock as mock


def my_open(filename):
    if filename == 'file.txt':
        content = "text file\ncontent"
    elif filename == 'second.txt':
        content = 'foobar'
    else:
        raise FileNotFoundError(filename)
    file_object = mock.mock_open(read_data=content).return_value
    file_object.__iter__.return_value = content.splitlines(True)
    return file_object

elif
链中,您为每个现有文件路径设置“文件内容”。

测试:

# standalone
open_patch = mock.patch('__main__.open', new=my_open)
open_patch.start()

file = open('file.txt')
assert file.read() == "text file\ncontent"
file.close()

open_patch.stop()

#with statement
with mock.patch('__main__.open', new=my_open):
    with open('second.txt') as file:
        assert file.read() == 'foobar'

    # as iterable
    with open('file.txt') as file:
        assert ['text file\n', 'content'] == list(file)

# function decorator
@mock.patch('__main__.open', new=my_open)
def test_patched_open():
    with open('second.txt') as file:
        assert file.readline() == 'foobar'

test_patched_open()

5
投票

您将创建两个“文件”模拟,并模拟

open
以在调用
open()
时按顺序返回它们。
side_effect
属性
可以让您做到这一点:

@patch('__builtin__.open')
def test_interface_mapping(self, mock_open):
    handle1 = MagicMock('file1').__enter__.return_value
    handle1.__iter__.return_value = ('aa', 'bb')
    handle2 = MagicMock('file2').__enter__.return_value
    handle2.__iter__.return_value = ('foo', 'bar')
    mock_open.return_value.side_effect = (handle1, handle2)

模拟的

open()
调用在调用时首先返回
handle1
,然后返回
handle2
。然后,任一对象都会使用模拟来响应
__enter__()
的调用,该模拟为
__iter__
调用返回给定的元组。


3
投票

您可以在补丁中使用它代替

mock_open
...

def get_mock_open(files: dict[str, str]):
    def open_mock(filename, *args, **kwargs):
        for expected_filename, content in files.items():
            if filename == expected_filename:
                return mock_open(read_data=content).return_value
        raise FileNotFoundError('(mock) Unable to open {filename}')
    return MagicMock(side_effect=open_mock)

使用

传入一个文件名:内容的字典,像这样...

def test_multiple_opens():
    content1 = 'abc'
    content2 = 'some\nlines\ncontent'
    files = {'file1': content1,
             'file2': content2}

    with patch('builtins.open', get_mock_open(files)) as open_mock:
        with open('file1', 'r') as file:
            result1 = file.read()
        with open('file2', 'r') as file:
            result2 = file.read()

    assert result1 == content1
    assert result2 == content2
    open_mock.assert_called_with('file2', 'r')

0
投票

这里有点晚了,但是这里有一个固定装置,可以让你以更清晰和现代的方式完成它,这要归功于最新的Python版本上的

mocker

@fixture()
def mock_files(mocker, request):
    if not hasattr(request, "param"):
        setattr(request, "param", [""])
    if not isinstance(request.param, list):
        request.param = [request.param]

    mock_files = mocker.patch("builtins.open", mock_open(read_data=request.param[0]))

    if len(request.param) > 1:
        mock_files.side_effect = (mock_open(read_data=param).return_value for param in request.param)

    yield mock_files
© www.soinside.com 2019 - 2024. All rights reserved.