Python 3 模拟对象支持查询其调用的参数,是否也可以查询它们的调用返回的值?
我的特殊情况是我模拟 tempfile.mkdtemp,但作为副作用调用真正的 mkdtemp。我想在我的测试中获取创建的临时目录。
from unittest import mock
import shutil
import tempfile
from app import production_function
def mkdtemp(*args, **kwargs):
dtemp = orig_mkdtemp(*args, **kwargs)
return dtemp
orig_mkdtemp = tempfile.mkdtemp
patcher = mock.patch('tempfile.mkdtemp', name='tempfile.mkdtemp')
the_mock = patcher.start()
the_mock.side_effect = mkdtemp
# Call function under test
production_function()
assert the_mock.called
# Now, how to get the return value from the call to the_mock?
patcher.stop()