I have two test cases that involve reading the same file (they call a function that loads a JSON file as classes).
The function is:
import os
import json
def load_json(relative_file_path: str) -> Dict[str, Any]:
current_file_path = os.path.dirname(__file__)
file_to_open_path = os.path.join(current_file_path, relative_file_path)
with open(file_to_open_path, 'r') as opened_file:
return json.load(opened_file)
When I run with pytest-xdist and -n=logical they fail but when I run serially (-n=0) they pass.
The failure message is:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I added a try catch around the load and I get:
Cannot load file to json, file is
I assume this means that the reader from one test is conflicting with the reader with the second causing it to appear to be at the end of the file for the second read.
Is there anything I do in opening the file to allow for parallel reads?
After some further digging it looks like another test was writing the file so I think I was getting conflicts