1

我现在正在尝试使用 mock.patch 模拟一个函数,例如:

with mock.patch.object(self.myClass, 'MyClassMethod', return_value=None) as mock_MyMethod:
    self.myClass.start()
    mock_MyMethod.assert_called_once_with()

现在我想让 MyClassMethod 打印“hello word!!” 当它被调用时。谁能帮我找到解决方案。

提前致谢,

4

1 回答 1

4

你可以使用side_effect. 首先你定义你的打印功能:

def hello():
    print("hello world!!!")
    return mock.DEFAULT

然后你像这样初始化你的模拟对象:

with mock.patch.object(..., side_effect=hello)
于 2015-03-07T18:19:09.053 回答