Python: Mocking a context manager

Posted on

Question :

Python: Mocking a context manager

I don’t understand why I can’t mock in this example:

from mock import Mock, patch
import unittest
import tempfile

def myfunc():
    with tempfile.NamedTemporaryFile() as mytmp:

class TestMock(unittest.TestCase):
    def test_cm(self, mock_tmp):
        mytmpname = 'abcde' = mytmpname
        self.assertEqual(myfunc(), mytmpname)

Test results in:

AssertionError: <MagicMock name='NamedTemporaryFile().__enter__().name' id='140275675011280'> != 'abcde'
Asked By: Willem


Answer #1:

You are setting the wrong mock: mock_tmp is not the context manager, but instead returns a context manager. Replace your setup line with: = mytmpname

and your test will work.

Answered By: Michele d’Amico

Answer #2:

Extending Peter K’s answer using pytest and the mocker fixture.

def myfunc():
    with tempfile.NamedTemporaryFile(prefix='fileprefix') as fh:

def test_myfunc(mocker):
    mocker.patch('tempfile.NamedTemporaryFile') = 'tempfilename'
    assert myfunc() == 'tempfilename'
Answered By: hmobrienv

Answer #3:

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' = mytmpname
    assert myfunc() == mytmpname
Answered By: Peter K

Answer #4:

To expand on Nathaniel’s answer, this code block

with tempfile.NamedTemporaryFile() as mytmp:

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. 

When you replace tempfile.NamedTemporaryFile with an instance of Mock or MagicMock

# This first line, below, will call mock_tmp().
# Therefore we need to set the return_value with
# mock_tmp.return_value
context_manager = mock_tmp()

# This will call mock_tmp.return_value.__enter__() so we need to set 
# mock_tmp.return_value.__enter__.return_value
mytmp = context_manager.__enter__()

# This will access
Answered By: FiddleStix

Answer #5:

I extended hmobrienv’s answer to a small working program

import tempfile
import pytest

def myfunc():
    with tempfile.NamedTemporaryFile(prefix="fileprefix") as fh:

def test_myfunc(mocker):
    mocker.patch("tempfile.NamedTemporaryFile") = "tempfilename"
    assert myfunc() == "tempfilename"

if __name__ == "__main__":
Answered By: David Howlett

Leave a Reply

Your email address will not be published.