'function' 对象没有属性 'assert_call_once_with'
- 作者: 那晚越女说我?
- 来源: 51数据库
- 2022-10-20
问题描述
我正在尝试使用 pytest 和 pytest_mock 运行以下测试
I'm trying to run the following test using pytest and pytest_mock
def rm(filename):
helper(filename, 5)
def helper(filename):
pass
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file', 5)
但我得到异常 AttributeError: 'function' object has no attribute 'assert_called_once_with'
我做错了什么?
推荐答案
你不能在 vanilla 函数上执行 .assert_call_once_with 函数:你首先需要包装它与 mock.create_autospec代码> 装饰器.比如:
You can not perform a .assert_called_once_with function on a vanilla function: you first need to wrap it with the mock.create_autospec decorator. So for instance:
import unittest.mock as mock
def rm(filename):
helper(filename, 5)
def helper(filename):
pass
helper = mock.create_autospec(helper)
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file', 5)
或者更优雅:
import unittest.mock as mock
def rm(filename):
helper(filename, 5)
@mock.create_autospec
def helper(filename):
pass
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file', 5)
请注意,断言将失败,因为您仅使用 'file' 调用它.所以一个有效的测试是:
Note that the assertion will fail, since you call it only with 'file'. So a valid test would be:
import unittest.mock as mock
def rm(filename):
helper(filename, 5)
@mock.create_autospec
def helper(filename):
pass
def test_unix_fs(mocker):
mocker.patch('module.helper')
rm('file')
helper.assert_called_once_with('file')
编辑:如果函数是在某个模块中定义的,您可以将其包装在本地的装饰器中.例如:
EDIT: In case the function is defined in some module, you can wrap it in a decorator locally. For example:
import unittest.mock as mock
from some_module import some_function
some_function = mock.create_autospec(some_function)
def test_unix_fs(mocker):
some_function('file')
some_function.assert_called_once_with('file')
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
