调用Python模拟函数时是否仍要打印语句?
我现在尝试使用mock.patch模拟函数,例如:调用Python模拟函数时是否仍要打印语句?,python,python-3.x,python-mock,Python,Python 3.x,Python Mock,我现在尝试使用mock.patch模拟函数,例如: with mock.patch.object(self.myClass, 'MyClassMethod', return_value=None) as mock_MyMethod: self.myClass.start() mock_MyMethod.assert_called_once_with() 现在我想让我的ClassMethod打印hello word!!当它被呼叫时。谁能帮我找到解决这个问题的办法 提前感谢,您可以
with mock.patch.object(self.myClass, 'MyClassMethod', return_value=None) as mock_MyMethod:
self.myClass.start()
mock_MyMethod.assert_called_once_with()
现在我想让我的ClassMethod打印hello word!!当它被呼叫时。谁能帮我找到解决这个问题的办法
提前感谢,您可以使用副作用。首先定义打印功能:
def hello():
print("hello world!!!")
return mock.DEFAULT
然后初始化模拟对象,如下所示:
with mock.patch.object(..., side_effect=hello)
你可以使用副作用。首先定义打印功能:
def hello():
print("hello world!!!")
return mock.DEFAULT
然后初始化模拟对象,如下所示:
with mock.patch.object(..., side_effect=hello)