Python mock builtin' open'在使用两个不同文件的类中

时间:2014-11-06 15:53:06

标签: python unit-testing mocking contextmanager

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

@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

3 个答案:

答案 0 :(得分:9)

您必须使用已修补的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])

<强> [编辑] 我找到了一种更优雅的方式Mock builtin 'open" function when used in contextlib

所以你可以像

一样重写测试
@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()而无需嘲笑其他东西。

答案 1 :(得分:3)

您将创建两个“文件”模拟,并模拟open以便在调用open()时按顺序返回这些模拟。 side_effect attribute可以让您这样做:

@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__调用的给定元组。

答案 2 :(得分:2)

如果您需要对文件内容进行更多控制,可以使用包装函数。 它根据文件名将文件内容替换为原始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()