I don\'t understand why I can\'t mock NamedTemporaryFile.name in this example:
from mock import Mock, patch import unittest import tempfile def myfunc():
You are setting the wrong mock: mock_tmp is not the context manager, but instead returns a context manager. Replace your setup line with:
mock_tmp
mock_tmp.return_value.__enter__.return_value.name = mytmpname
and your test will work.