【问题标题】:Where am I going wrong with patching a function with mock_open?用 mock_open 修补函数我哪里出错了?
【发布时间】:2019-08-15 20:38:44
【问题描述】:

我有一个调用子函数来打开文件的函数。我正在尝试测试父函数,但我想修补子函数并让它返回我传入的数据(就像它从文件中读取一样)。

tests.py

# Read in the sample data
__SAMPLE_LOG = os.path.join(settings.BASE_DIR, "apps/tests/log_viewer/sample_logs/sample_manager_log.log")
sample_data = []
for line in reversed_lines(open(__SAMPLE_LOG)):
    sample_data.append(line)

sample_data = ('').join(sample_data)

class ReadLog(TestCase):
    @patch('apps.log_viewer.utils.reversed_lines', new_callable = mock_open, read_data = sample_data)
    def test_returnsDictionaryContainingListOfDictionaries(self, mock_file):
        activity = read_log()

        # Make sure the sample data was read ==> this fails.
        self.assertEqual(open(settings.ACTIVITY_LOG_FILE).read(), sample_data)

utils.py

def read_log():

   # This is the line I am trying to patch
   for line in reversed_lines(open(settings.ACTIVITY_LOG_FILE)):      
      # process data

# see: https://stackoverflow.com/questions/260273/most-efficient-way-to-search-the-last-x-lines-of-a-file-in-python/260433#260433
def reversed_lines(file):
    "Generate the lines of file in reverse order."
    part = ''
    for block in reversed_blocks(file):
        for c in reversed(block):
            if c == '\n' and part:
                yield part[::-1]
                part = ''
            part += c
    if part: yield part[::-1]

def reversed_blocks(file, blocksize=4096):
    "Generate blocks of file's contents in reverse order."
    file.seek(0, os.SEEK_END)
    here = file.tell()
    while 0 < here:
        delta = min(blocksize, here)
        here -= delta
        file.seek(here, os.SEEK_SET)
        yield file.read(delta)

错误

我正在尝试在read_log() 方法中修补utils.py 中的reversed_lines(),但read_log() 仍在从实际日志中读取,这表明我没有正确修补reversed_lines()

当我改变时

@patch('apps.log_viewer.utils.reversed_lines', new_callable = mock_open, read_data = sample_data)

@patch('builtins.open', new_callable = mock_open, read_data = sample_data)

我明白了

======================================================================
ERROR: test_returnsDictionaryContainingListOfDictionaries 
(tests.log_viewer.test_utils.ReadLog)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1209, in patched
    return func(*args, **keywargs)
  File "/webapp/apps/tests/log_viewer/test_utils.py", line 32, in test_returnsDictionaryContainingListOfDictionaries
    activity = read_log()
  File "/webapp/apps/log_viewer/utils.py", line 64, in read_log
    for line in reversed_lines(open(settings.ACTIVITY_LOG_FILE)):
  File "/webapp/apps/log_viewer/utils.py", line 173, in reversed_lines
    for block in reversed_blocks(file):
  File "/webapp/apps/log_viewer/utils.py", line 164, in reversed_blocks
    while 0 < here:
TypeError: '<' not supported between instances of 'int' and 'MagicMock'

我哪里错了?

【问题讨论】:

  • 你能提供一个独立的例子吗?我有相当多的模拟经验,但在当前的 sn-ps 中,我缺少导入和目录结构,这使得我无法开始帮助你

标签: python unit-testing python-unittest


【解决方案1】:

按照https://docs.python.org/3.3/library/unittest.mock.html#mock-open 文档中的示例,我想你想要

@patch('builtins.open', mock_open(read_data = sample_data), create=True)

不过通读mock_open的源码:https://github.com/python/cpython/blob/3.7/Lib/unittest/mock.py#L2350

看起来文件句柄的tell 方法没有被mock 实现。唯一受支持的方法是readreadlinereadlineswrite 并迭代内容。您需要为 tell 方法手动设置模拟。这不是一般的实现,但可以在您的特定情况下使用:

class ReadLog(TestCase):
    @patch('builtins.open', mock_open(read_data = sample_data), create=True)
    def test_returnsDictionaryContainingListOfDictionaries(self, mock_file):
        mock_file.return_value.tell.return_value = len(sample_data)
        ...

【讨论】:

  • 这对我不起作用。它说 mock_file 参数没有传入,然后当我删除它时,我得到 TypeError: '&lt;' not supported between instances of 'int' and 'MagicMock'
  • @Hunter 查看我的更新 - python 的 mock_open 实现未实现 tell 方法,因此您需要手动模拟适合您的测试
  • 感谢这真的很接近!我将您的补丁行更改为 @patch('builtins.open', new_callable = mock_open, read_data = sample_data) 并且有效!使用您的补丁行我收到错误Traceback (most recent call last): File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1209, in patched return func(*args, **keywargs) TypeError: test_returnsMessagesNewestFirst() missing 1 required positional argument: 'mock_file'
猜你喜欢
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2013-12-25
  • 2018-01-21
  • 1970-01-01
  • 2014-03-01
相关资源
最近更新 更多