简体   繁体   中英

Python Mockito: How do I mock open()

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)

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

import builtins

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM