Python 用mock_open修补函数会有什么问题?
我有一个函数,它调用一个子函数来打开一个文件。我试图测试父函数,但我想修补子函数,让它返回我传入的数据(就像它从文件读取一样) 测试.pyPython 用mock_open修补函数会有什么问题?,python,unit-testing,python-unittest,Python,Unit Testing,Python Unittest,我有一个函数,它调用一个子函数来打开一个文件。我试图测试父函数,但我想修补子函数,让它返回我传入的数据(就像它从文件读取一样) 测试.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://stackoverflo
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)
#读取样本数据
__SAMPLE\u LOG=os.path.join(settings.BASE\u DIR,“apps/tests/LOG\u viewer/SAMPLE\u logs/SAMPLE\u manager\u LOG.LOG”)
样本_数据=[]
对于反向行中的行(打开(uuu示例_u日志)):
示例_数据。追加(行)
样本数据=('').join(样本数据)
类ReadLog(TestCase):
@补丁('apps.log\u viewer.utils.reversed\u line',new\u callable=mock\u open,read\u data=sample\u data)
def test_ReturnsDictionary包含字典列表(自我,模拟文件):
活动=读取日志()
#确保已读取样本数据==>此操作失败。
self.assertEqual(打开(settings.ACTIVITY\u LOG\u FILE.read(),sample\u数据)
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)
到
我明白了
======================================================================
错误:测试返回Dictionary包含Dictionary列表
(tests.log\u viewer.test\u utils.ReadLog)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“/usr/local/ceral/python/3.7.4/Frameworks/python.framework/Versions/3.7/lib/python3.7/unittest/mock.py”,第1209行,带补丁
返回函数(*参数,**键盘)
文件“/webapp/apps/tests/log_viewer/test_utils.py”,第32行,在test_returns字典目录中
活动=读取日志()
文件“/webapp/apps/log\u viewer/utils.py”,第64行,在读取日志中
对于反向行中的行(打开(settings.ACTIVITY\u LOG\u文件)):
文件“/webapp/apps/log\u viewer/utils.py”,第173行,反向行
对于反向块(文件)中的块:
文件“/webapp/apps/log\u viewer/utils.py”,第164行,反向块
而0<此处:
TypeError:“以下是我认为您需要的
@patch('builtins.open', mock_open(read_data = sample_data), create=True)
然而,通过mock\u open
的源代码阅读:
似乎文件句柄的tell
方法不是由mock实现的。唯一受支持的方法是read
、readline
、readlines
、write
和迭代内容。您需要手动设置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_文件
参数没有传入,然后当我删除它时,我得到了TypeError:“@Hunter查看我的更新-python的mock_open
实现没有实现tell
方法,所以你需要手动模拟它,因为它适合你的测试。谢谢,这真的很接近!我将您的补丁行更改为@patch('builtins.open',new\u callable=mock\u open,read\u data=sample\u data)
,它成功了!使用您的补丁行,我得到了错误回溯(最近一次调用):File“/usr/local/cillar/python/3.7.4/Frameworks/python.framework/Versions/3.7/lib/python3.7/unittest/mock.py”,第1209行,在补丁返回函数(*args,**keywargs)中类型错误:test_returnsmessnewestfirst()缺少1个必需的位置参数:“mock_file”
能否提供一个自包含的示例?我在模拟方面有相当多的经验,但在当前的代码片段中,我缺少导入和目录结构,这使得我需要做很多工作才能开始帮助您