pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.4k stars 345 forks source link

`pytest.PytestConfigWarning: could not load initial conftests` with --help or -V but not when running tests #1152

Open johncarter-phntm opened 1 month ago

johncarter-phntm commented 1 month ago

I've got some fixtures loaded with pytest_plugins, these fixtures were doing a top level import of models.

So this was triggering an issue due to early import of the models. I've resolved by removing the top level imports. What's odd is that running the tests wasn't hitting the issue, presumably because of a different code path for the test run vs --help.

Is there a workaround to force django.setup() in --help etc?

With:

pytest==8.3.3
pytest-django==4.9.0

Full traceback:

Traceback (most recent call last):
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 710, in _importconftest
    mod = import_path(
          ^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/pathlib.py", line 582, in import_path
    importlib.import_module(module_name)
  File "/home/johnc/.pyenv/versions/3.12.6/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/assertion/rewrite.py", line 184, in exec_module
    exec(co, module.__dict__)
  File "/home/johnc/Projects/phq-akl/conftest.py", line 8, in <module>
    from django.contrib.auth import models as group_models
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1398, in _preparse
    self.hook.pytest_load_initial_conftests(
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/warnings.py", line 151, in pytest_load_initial_conftests
    return (yield)
            ^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/capture.py", line 154, in pytest_load_initial_conftests
    yield
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1222, in pytest_load_initial_conftests
    self.pluginmanager._set_initial_conftests(
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 581, in _set_initial_conftests
    self._try_load_conftest(
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 619, in _try_load_conftest
    self._loadconftestmodules(
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 659, in _loadconftestmodules
    mod = self._importconftest(
          ^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 718, in _importconftest
    raise ConftestImportFailure(conftestpath, cause=e) from e
_pytest.config.ConftestImportFailure: AppRegistryNotReady: Apps aren't loaded yet. (from /home/johnc/Projects/phq-akl/conftest.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/bin/pytest", line 8, in <module>
    sys.exit(console_main())
             ^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 201, in console_main
    code = main()
           ^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 156, in main
    config = _prepareconfig(args, plugins)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 341, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/helpconfig.py", line 105, in pytest_cmdline_parse
    config = yield
             ^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1140, in pytest_cmdline_parse
    self.parse(args)
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1494, in parse
    self._preparse(args, addopts=addopts)
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1405, in _preparse
    self.issue_config_time_warning(
  File "/home/johnc/.cache/pypoetry/virtualenvs/phq-akl-cnWCqvK--py3.12/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1532, in issue_config_time_warning
    warnings.warn(warning, stacklevel=stacklevel)
pytest.PytestConfigWarning: could not load initial conftests: /home/johnc/Projects/phq-akl/conftest.py