I have the next tree:
root_project/
├── app
│ ├── default_photo_profile.jpg
│ ├── config.py
│ ├── __main__.py # My app are python package, I'm runnig it via "python -m"
│ └── ...
├── tests
│ ├── test_unit.py # import config.py inside
│ ├── functional # import config.py inside
│ ├── pytest.ini
│ └── ...
...
Currently default_photo_profile
causing error because tests
doesn't have this file.
Reading file in config.py
:
DEFAULT_PHOTO_FILE_PATH = Path('default_photo.jpg')
with open(file=DEFAULT_PHOTO_FILE_PATH, mode='rb') as file_obj:
DEFAULT_PHOTO_BYTES = file_obj.read()
How I can solve this?
I tried:
- Patch access to
default_photo.jpg
with fixture - not helped, error during import stage, not executiion. - set flag to pytest comamnd line:
--rootdir app
- not helped (don't know why). try/except
for reading the file inapp.config.py
- may help but it's not my intention, I really want raise error if file not found- Put
default_photo.jpg
inside EVERY test directory - will help bit dirty. - Patch
os.path
like suggested in https://stackoverflow.com/a/43003192/11277611 - dirty - Include tests into package (move
__main__.py
intoroot_project
- not sure that it's a good idea (have not enough experience to decide). - Set absolut path to
default_photo.jpg
- will fail on the production server.
Probably adoptable solutions (What I want):
- Set root dir to
root_project.app
somehow insidepytest.ini
to immitate regular execution. - Set root dir to
root_project.tests
somehow to place file in root of tests and access from any oftests
folder.