📖

srcディレクトリのモジュールがpytestでModuleNotFoundErrorエラーになる問題を解決

に公開

結論

pyproject.tomlに以下を追加する

[tool.pytest.ini_options]
pythonpath = ["src"]

testsディレクトリ直下に__init__.pyを置く

/
├─ src
│  ├─ module
│  │  └─ file.py
│  └─ main.py
└─ tests
   ├─ test_module
   │  └─ test_file.py
   ├─ __init__.py // 追加
   └─ test_main.py

参考記事

https://zenn.dev/zerebom/articles/3ee57dc587b6d3

Discussion