I have some Python code that loads in a local JSON file:
with open("/path/to/file.json") as f:
json_str = f.read()
# Now do stuff with this JSON string
In testing, I want to patch that JSON file to be a JSON file located in my repo's test directory ("/path/to/repo/test/fake_file.json"
).
How can I go about doing that?
One other requirement is I actually have a version of "/path/to/file.json"
locally, but I don't want to change it. I want it patched over at test time, and unpatched upon test completion.
Note: I use pytest
, and it seems like the plug-in pyfakefs
would do this. Sadly, I can't figure out how to get it to patch in another local file (from within my repo's test directory). I am open to solutions using vanilla Python 3.10+ and/or pyfakefs
.