Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未调用Pytest模拟补丁函数_Python_Pytest_Python Mock - Fatal编程技术网

Python 未调用Pytest模拟补丁函数

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,

我确信调用了一个函数(因为函数上有一个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, 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!
}