【发布时间】:2020-11-06 21:10:26
【问题描述】:
我正在尝试模拟在 S3 存储桶中打开文件的调用。我的代码是:
# mymodule.py
import s3fs
#...
def __init__(self):
self.s3_filesystem = s3fs.S3FileSystem(anon=False, key=s3_key,
secret=s3_secret)
#...
def mymethod(self):
with self.s3_filesystem.open(filename, 'r') as csv_file:
file_dataframe = pd.read_csv(csv_file)
我在 pytest 中的模拟是:
import pytest
def test(mocker)
mocker.patch('mypackage.mymodule.s3fs.S3FileSystem.open',
return_value=open)
但在运行测试时出现错误:
> with self.s3_filesystem.open(filename, 'r') as csv_file:
E AttributeError: __enter__
知道为什么吗?
【问题讨论】:
-
我通过使用 side_effect=open 找到了解决方案,而不是使用 return_value=open。 mocker.patch('mypackage.mymodule.s3fs.S3FileSystem.open', side_effect=open) 见fgimian.github.io/blog/2014/04/10/…
标签: python mocking pytest python-s3fs