I have created a project exactly as described by the pytest Good Integration Practices documentation, specifically the Tests outside application code section.
This is my project layout
.
├── pyproject.toml
├── src
│ └── basic_package
│ ├── bar.py
│ ├── __init__.py
│ └── main.py
└── tests
└── test_app.py
In main.py I import from bar.py
from bar import baz
def main() -> str:
return baz()
in test_app.py
from basic_package.main import main
def test_foo():
assert main() == 'qux'
It works when I run the project. However, if I run pytest, I get an error
ModuleNotFoundError: No module named 'bar'
It works in pytest if I change the code in main.py to
from .bar import baz
But then if I run the application I get the ModuleNotFoundError
How can I resolve this issue?
I am running pytest from the root directory of the project
The solution to this dilemma was to investigate sys.path for the two cases
When running the package it was:
['.../basic_app/src/basic_package',
...]
and running pytest
['.../basic_app/src',]
Therefore, I had to change the pythonpath key in the tool.pytest.ini_options table in pyproject.toml from
pythonpath = [
"src"
]
to
pythonpath = [
"src/basic_package"
]
And now both cases pick up
from bar import baz