I am storing all the pytest files in the tests
sub-directory under the project root. There is no __init__.py
in that directory or above and pytest tests/test_a.py
works as expected. I can also run pytest test_a.py
directly from within the tests
folder.
|--<project_root>
+---[tests]
test_a.py
base.py
config.py
conftest.py
The test class in test_a.py
inherits from base.py
. Now, the problem is that because of the missing __init__.py
the IDE tooling does not work and the imports cannot be resolved. Adding __init__.py
under the tests
resolves all import errors in the IDE, but tests won't run anymore with pytest test_a.py
because py.test fails to import conftest.py
.
In my conftest.py
I have the following imports:
from config import HOST_URL_DEFAULT, USER, PASSWORD
which results in:
ModuleNotFoundError: No module named 'config'
ERROR: could not load /project_root/tests/conftest.py
Is there a way to solve this so that both IDE tooling works and pytest
keeps working? I would like to avoid using dot imports, if possible.
Update: After reading this answer I think I am finally starting to understand a bit more how python imports work.