我确定某个函数被调用了(因为在该函数中有一个打印语句)。
我的测试目标是函数 handle_action
。
__init__.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_nothing
改为handlers.handle_nothing
,我会收到相同的错误。