Skip to content

Pytest issues #44

Open
Open
@jmwebb

Description

@jmwebb

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions