So you want to mock python’s open.

It’s kind of silly. You can’t use the decorator form of mock.patch since this version doesn’t return anything, so you have to use the context manager version.

mock_open = mock.mock_open()
with mock.patch('your.module.path.open', mock_open, create=True):
    thing.you_are_testing_that_uses_open(your, params, here)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>