Jeremy Satterfield
Coding, Brewing and Tulsa Life

Mocking Context Managers in Python

I've often found Python's context managers to be pretty useful. They make a nice interface that can handle starting and ending of temporary things for you, like opening and closing a file.

For example:

f = open('myfile.txt', 'w')
    for row in records:

can be replaced with

with open('myfile.txt', 'w') as f:
    for row in records:

This last week I was working with the ZipFile module and wanted to use it's context manger interface, but I ran into a little confusion when it came to unit testing. After a little better understanding of how context managers work, I figured out that the __enter__ and __exit__ methods are what really makes a context handler. As explained at PyMOTW, when you invoke with on a class, __enter__ is called and should return an object to be used in the context (f in the above example), the code within the block is executed, and __exit__ is called no matter the outcome of the block. So both of these would be roughly equivalent, assuming do_stuff doesn't raise an exception.

with Context(foo) as bar:

c = Context(foo)
bar = c.__enter__()
    c.__exit__(None, None, None)

With this understanding, here is the solution to my mocking problem using PyMox.

def zip_it_up(filename):
    with ZipFile(filename, 'w') as f:
        for file in FILES:

... # setup and stuff is up here somewhere
def test_building_zipfile(self):
    self.mock.StubOutWithMock(module.tasks, 'ZipFile')
    mock_zip = self.mock.CreateMockAnything()
    module.tasks.ZipFile('/tmp/', 'w').AndReturn(mock_zip)
    mock_zip.write('/tmp/export-1.xml', 'export-1.xml')
    mock_zip.write('/tmp/export-2.xml', 'export-2.xml')
    mock_zip.__exit__(None, None, None)


Mocking out ZipFile allows us to return a mock object from it's instantiation. We can then set the expectation that __enter__ will be called on the instance, returning the instance itself, expecting write to be called twice on the instance and finally __exit__ to be called. The three arguments of None here are to indicate that an exception isn't expected. If the code inside the context block were to raise an exception, these arguments would be the type, value and traceback as returned by raise. In the event you are testing for an exception, these arguments should be set accordingly when setting expectations.