whythawk / full-stack-fastapi-postgresql

Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Nuxt3, Docker, automatic HTTPS and more.
MIT License
231 stars 47 forks source link

Pytest issues #44

Open jmwebb opened 7 months ago

jmwebb commented 7 months ago

Been having a ton of trouble running the scripts/test-local.sh as instructed in the README. I keep running into ModulenotFoundError issues in backend/app/app/tests/conftest.py I've confirmed the following are working:

  1. My backend Docker container is copying over backend/app to /app as expected.
  2. /app is getting set as the Python path as expected.
  3. Imports work normally when bash'd into the backend container.
  4. Imports work normally when running the backend service.
  5. sys.path contains /app as expected (I print it out while running pytest -s).

However, no matter how I seem to run pytest (bash'd into the backend service, docker compose run backend pytest, or the test-local.sh script), I get the following error:

==================================================================================================== ERRORS ====================================================================================================
________________________________________________________________________________________ ERROR collecting test session _________________________________________________________________________________________
/usr/local/lib/python3.10/site-packages/_pytest/config/__init__.py:641: in _importconftest
    mod = import_path(conftestpath, mode=importmode, root=rootpath)
/usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/usr/local/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
    ???
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:688: in _load_unlocked
    ???
/usr/local/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:186: in exec_module
    exec(co, module.__dict__)
app/tests/conftest.py:14: in <module>
    from app.core.config import settings
E   ModuleNotFoundError: No module named 'app.core'

When I try to append an extra app. to the imports (e.g., from app.app.core.config import settings, the error goes away, but then my regular backend service crashes (ModuleNotFoundError app.app). Clearly it is some difference between how pytest and running the service regularly handle imports.

My file structure is the same as the template project: the main.py for FastAPI lives at backend/app/app, my pyproject.toml lives at backend/app.

I am running pytest 7.1.3, python 3.10, and Mac OS 14.1.

turukawa commented 7 months ago

Hey @jmwebb... I've kind-of left the tests stranded since refreshing this stack, so thanks for looking at this.

I vaguely remember a similar issue on the main stack when I first started working on this. First question ... some of these weirder glitches on the .sh files happen because of Windows vs Unix line-endings. Could that be related? After that, I'm not sure ... I'll have a look and see whether I can come up with anything.

turukawa commented 7 months ago

Right, I've just run a full new build from cookiecutter to see what happens. @jmwebb I'd be interested to know how you set things up? pytest isn't one of the dependencies (sadly, when I did the migration from Poetry to Hatch, I must have left all the test requirements out). For the sake of speed, I simply added it to the main dependencies.

In test.sh, the command to launch pytest is:

pytest --cov=app --cov-report=term-missing app/tests "${@}"

But that fails. I'm not sure what either of the --cov parameters does. Removing them results in the following error:

_______________________________________________________________________________________________________ ERROR collecting app/tests/crud/test_item.py _______________________________________________________________________________________________________
ImportError while importing test module '/app/app/tests/crud/test_item.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
app/tests/crud/test_item.py:4: in <module>
    from app.schemas.item import ItemCreate, ItemUpdate
E   ModuleNotFoundError: No module named 'app.schemas.item'

Which is not surprising, because the item example doesn't exist in this build. So ... what I'm not replicating is you app.core error.

Is this an error from this base stack, or is yours already your custom development?