1
如何在功能上使用mock.patch
,因此我可以访问方法.assert_called
等,同时我仍然可以保留函数的原始功能?Python中的部分补丁与模拟
下面是示例代码:
from unittest import mock
def foo(arg):
print(arg)
def tested():
foo('hi')
@mock.patch('__main__.foo')
def test(foo):
tested()
foo.assert_called_once()
test()
我希望它测试,如果foo
函数被调用只有一次,但我仍然需要它来打印hi
。