As a follow up to a previous question I would like to have a mock open
function in a separate file be able to use the builtin open
function. Again, here are the files:
main.py
mock.py
test.txt
test_main.py
main.py
contains the following:
fs = open('test.txt', 'r')
mock.py
now contains the following:
import builtins
def open(filename, mode):
fs = builtins.open(filename, mode)
print(fs.readline())
fs.close()
test.txt
contains the following:
abc
def
ghi
test_main.py
contains the following:
import pathlib
import mock
import builtins
if __name__=="__main__":
file_path = pathlib.Path('test.txt')
builtins.open = mock.open
import main
Unfortunately when I run python test_main.py
I get the following error:
RecursionError: maximum recursion depth exceeded
Which indicates to me that mock.open
is not using the built-in open
in its body....
How do I mock open
for main.py
and all its imports, but have mock.open
be able to use the built-in open
?