I have this structure:
web/
api/
app.py
sets.json
tests/
test.py
app.py:
def func():
with open('sets.json', 'r') as file:
...
test.py:
import sys
import os
sys.path.append(os.getcwd()+'/api/')
from app import func
...
I want to run tests from the root (web/). Import is successful. But when func are called, I get error: FileNotFoundError: [Errno 2] No such file or directory: 'sets.json'
.
Why? Sys.path was changed and import is worked. Why I can't import sets.json?
As a result, I have got such solution:
I create tests/conftest.py
(for Pytest, it allows to apply my changes for all files) with code:
import sys
import os
os.chdir(os.getcwd()+'/api/')
sys.path.append(os.getcwd())
With this solution I can do imports and from web/api/
(in Docker) and from web/
(in PyTest) and get access to sets.json
in web/api/
.