Python中带有m的部分补丁

2024-04-25 04:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在一个函数上使用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: 方法函数代码test功能示例foodef