Python 未调用Pytest模拟补丁函数
我确信调用了一个函数(因为函数上有一个print语句) 我的测试目标是名为Python 未调用Pytest模拟补丁函数,python,pytest,python-mock,Python,Pytest,Python Mock,我确信调用了一个函数(因为函数上有一个print语句) 我的测试目标是名为\uuu init\uuu.py的文件上的函数handle\u action 另外,我猜模块的名称(\uuuu init\uuuuu.py)是问题的根源 from .dummy import HANDLE_NOTHING, handle_nothing handlers = { HANDLE_NOTHING: handle_nothing, } def handle_action(action, user,
\uuu init\uuu.py的文件上的函数handle\u action
另外,我猜模块的名称(\uuuu init\uuuuu.py
)是问题的根源
from .dummy import HANDLE_NOTHING, handle_nothing
handlers = {
HANDLE_NOTHING: handle_nothing,
}
def handle_action(action, user, room, content, data):
func = handlers.get(action)
if func:
func(user, room, content, data)
还有我的测试
import mock
from handlers import HANDLE_NOTHING, handle_action
def test_handle_dummy_action():
action = HANDLE_NOTHING
user = "uid"
room = "room"
content = "test"
data = {}
with mock.patch("handlers.dummy.handle_nothing") as f:
handle_action(action, user, room, content, data)
f.assert_called_with()
当我跑步时,我得到:
E AssertionError: expected call not found.
E Expected: handle_nothing()
E Actual: not called.
如果我从handlers.dummy.handle\u nothing
更改为handlers.handle\u nothing
我也会遇到同样的错误
我不知道,其他的模拟很好用
也许是文件名?(代码在\uuuu init\uuuuuuy.py
中)问题是您太迟了,无法修补,在创建处理程序
dict时,即导入代码时,名称已在导入时解决:
处理程序={
什么都不要处理:什么都不要处理,#哦!这很有道理。谢谢!
handlers = {
HANDLE_NOTHING: handle_nothing, # <-- name lookup of "handle_nothing" happens now!
}