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()