Reputation: 3253
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():
with tempfile.NamedTemporaryFile() as mytmp:
return mytmp.name
class TestMock(unittest.TestCase):
@patch('tempfile.NamedTemporaryFile')
def test_cm(self, mock_tmp):
mytmpname = 'abcde'
mock_tmp.__enter__.return_value.name = mytmpname
self.assertEqual(myfunc(), mytmpname)
Test results in:
AssertionError: <MagicMock name='NamedTemporaryFile().__enter__().name' id='140275675011280'> != 'abcde'
Upvotes: 132
Views: 92178
Reputation: 21
Another possibility is to use a factory to create an object that implements the context manager interface:
import unittest
import unittest.mock
import tempfile
def myfunc():
with tempfile.NamedTemporaryFile() as mytmp:
return mytmp.name
def mock_named_temporary_file(tmpname):
class MockNamedTemporaryFile(object):
def __init__(self, *args, **kwargs):
self.name = tmpname
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
return MockNamedTemporaryFile()
class TestMock(unittest.TestCase):
@unittest.mock.patch("tempfile.NamedTemporaryFile")
def test_cm(self, mock_tmp):
mytmpname = "abcde"
mock_tmp.return_value = mock_named_temporary_file(mytmpname)
self.assertEqual(myfunc(), mytmpname)
Upvotes: 2
Reputation: 3731
To expand on Nathaniel's answer, this code block
with tempfile.NamedTemporaryFile() as mytmp:
return mytmp.name
effectively does three things
# Firstly, it calls NamedTemporaryFile, to create a new instance of the class.
context_manager = tempfile.NamedTemporaryFile()
# Secondly, it calls __enter__ on the context manager instance.
mytmp = context_manager.__enter__()
# Thirdly, we are now "inside" the context and can do some work.
return mytmp.name
When you replace tempfile.NamedTemporaryFile
with an instance of Mock
or MagicMock
context_manager = mock_tmp()
# This first line, above, will call mock_tmp().
# Therefore we need to set the return_value with
# mock_tmp.return_value
mytmp = context_manager.__enter__()
# This will call mock_tmp.return_value.__enter__() so we need to set
# mock_tmp.return_value.__enter__.return_value
return mytmp.name
# This will access mock_tmp.return_value.__enter__.return_value.name
Upvotes: 44
Reputation: 109
I extended hmobrienv's answer to a small working program
import tempfile
import pytest
def myfunc():
with tempfile.NamedTemporaryFile(prefix="fileprefix") as fh:
return fh.name
def test_myfunc(mocker):
mocker.patch("tempfile.NamedTemporaryFile").return_value.__enter__.return_value.name = "tempfilename"
assert myfunc() == "tempfilename"
if __name__ == "__main__":
pytest.main(args=[__file__])
Upvotes: 3
Reputation: 209
Extending Peter K's answer using pytest and the mocker fixture.
def myfunc():
with tempfile.NamedTemporaryFile(prefix='fileprefix') as fh:
return fh.name
def test_myfunc(mocker):
mocker.patch('tempfile.NamedTemporaryFile').return_value.__enter__.return_value.name = 'tempfilename'
assert myfunc() == 'tempfilename'
Upvotes: 18
Reputation: 2484
Here is an alternative with pytest and mocker fixture, which is a common practice as well:
def test_myfunc(mocker):
mock_tempfile = mocker.MagicMock(name='tempfile')
mocker.patch(__name__ + '.tempfile', new=mock_tempfile)
mytmpname = 'abcde'
mock_tempfile.NamedTemporaryFile.return_value.__enter__.return_value.name = mytmpname
assert myfunc() == mytmpname
Upvotes: 6
Reputation: 23741
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.return_value.__enter__.return_value.name = mytmpname
and your test will work.
Upvotes: 214