Python imports can get weird, specially when dealing with tests that are to be kept separate from the application code (i.e., in sibling directories).
This is how you organize your tests with pytest
.
pkg/
code.py
tests/
__init__.py
test_code.py
# pkg/code.py
def f(x):
return x+2
# tests/__init__.py
# (may be left empty)
# tests/test_code.py
from pkg.code import f
def test_f():
assert f(2) == 4
Now you can run your tests with the pytest
command 😊