Skip to content Skip to sidebar Skip to footer

Why Does Mocking 'open' And Returning A Filenotfounderror Raise Attributeerror: __exit__?

Testing by mocking open with a FileNotFoundError raises AttributeError: __exit__. Why is this happening and what can I do to fix it? The following code opens a simple text file. If

Solution 1:

You mock the function to return an exception instead of raise it.

Try using side_effect (docs):

self.mock_open.side_effect = FileNotFoundError

Post a Comment for "Why Does Mocking 'open' And Returning A Filenotfounderror Raise Attributeerror: __exit__?"