在Python 3中使用mock的功能,我想包装一个函数并让它执行所有正常功能,我只是想模拟它的返回值。换句话说,我不想嘲笑整个函数,只是它的返回值,而且我不知道如何实现这一点。
def func():
# do something
# do something else
# do lots of other things
retval = 123
return retval
with mock.patch('func') as mocked:
mocked.return_value = 456
# And now, I want to run the function so that when
# it's called, "do something", "do something else",
# and "do lots of other things" still get performed,
# but that 456 gets returned instead of 123.
答案 0 :(得分:-1)
我发现了这个,我以前忽略过,这让我可以做我想做的事情:
How to call mocked method in Python mock
感谢jonrsharpe帮助我澄清我实际需要的内容。
这只是正在执行的许多单元和集成测试之一,我理解它本身的价值有限(但并非不存在)。