How do I have to arrange my files so python -m pytest
finds the classes in them?
I have the following strucutre (following the pytest docs convention) and several stackoverflow posts (this or this for example):
├─ testapp
│ ├─ module
│ │ ├─ __init__.py
│ │ ├─ app.py
│ ├─ test
│ │ ├─ test_app.py
The files contain the following:
# __init__.py
from .app import App
# app.py
class App:
def __init__(self):
pass
def add(self, a, b):
return a + b
# test_app.py
import sys
import pytest
# sys.path.insert(0, '../') # doesn't make any difference
# sys.path.insert(0, '../module') # doesn't make any difference
# print(sys.path) # shows that testapp/ is in the path no matter if the two lines above are included or not
import module.App # doesn't work
class TestApp:
def test_add():
app = module.App()
assert app.add(1, 2) == 3
When I run python -m pytest
in the testapp/
(it even sais it is ececuted with the root on testapp/
) I get the error: ModuleNotFoundError: No module named 'module.App'.
I tried module.app.App
with and without removing the code in the __init__.py
. I tried inserting .
and ..
and ../module
to the path before importing. I tried to add __init__.py
s in the testapp/
and/or in the testapp/test
. I tried adding a conftest.py
. Nothing works. But this is the "Good Integration Practices" (cite from pytest) so this should be working.
What am I doing wrong? What do I have to do to make it work?