Python Mockito: How do I mock open()

Viewed 291

This is the code I want to test

file_handle = open('filename')
content = file_handle.read()

Now, in my test file, I want to mock open()

I tried using mockito when as below

when(open(...)).thenReturn(dummy_response)
1 Answers

I found a way, which is not straightforward but works like a charm

import builtins

when(builtins).open(...).thenReturn(dummy_response)
Related