【发布时间】:2021-09-30 07:49:58
【问题描述】:
我正在使用 pytest-mock,但是抛出的异常来自 mock.patch 代码,我已经验证如果我使用 @mock.patch 装饰器语法会发生同样的错误。
MRE(确保您已安装 pytest 和 pytest-mock,无需导入任何内容):
def test_image(mocker):
mocker.patch("PIL.Image.save")
现在在这个模块上运行 pytest。
错误:
E AttributeError: <module 'PIL.Image' from 'c:\\users\\...\\site-packages\\PIL\\Image.py'> does not have the attribute 'save'
我可以清楚地看到Image.py确实包含一个名为save的函数,但是函数不被视为属性吗?我从来没有听说过这个词用于模块的内容。
【问题讨论】: