如何在一个函数上使用mock.patch
,这样我就可以访问方法.assert_called
等,同时还能保留函数的原始功能?在
下面是示例代码:
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
。在
Tags:
哦。我已经解决了。我只需要将参数
side_effect
添加到decorator:—)像这样:
相关问题 更多 >
编程相关推荐