pytest-django
pytest-django copied to clipboard
`pytest.PytestConfigWarning: could not load initial conftests` with --help or -V but not when running tests
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
Any suggestions on this one?
Hey guys, i'm trying to replicate this with no luck. I get a warning, but nothing "fails" or "crashes". Can you provide more details to this issue?
This is my test case
@pytest.mark.django_project(
create_manage_py=True,
extra_settings="""
INSTALLED_APPS = [
'tpkg.app',
]
"""
)
def test_django_bad_conftest(
django_pytester: DjangoPytester,
) -> None:
django_pytester.makeconftest(
"""
from tpkg.app.models import Item
from django.core.exceptions import AppRegistryNotReady
raise AppRegistryNotReady("Bad conftest")
"""
)
django_pytester.makepyfile(
"""
def test_always_fail():
assert False
"""
)
result = django_pytester.runpytest_subprocess("--help", "-vv")
print(result.stdout.str())
assert False
# result.stdout.fnmatch_lines(
# [
# "bad--super bad",
# ]
# )
$ pytest -vv -s tests/test_initialization.py::test_django_bad_conftest
...
usage: __main__.py [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
general:
-k EXPRESSION Only run tests which match the given substring
expression. An expression is a Python evaluable
expression where all names are substring-matched against
test names and their parent classes. Example: -k
'test_method or test_other' matches all test functions
and classes whose name contains 'test_method' or
'test_other', while -k 'not test_method' matches those
that don't contain 'test_method' in their names. -k 'not
test_method and not test_other' will eliminate the
matches. Additionally keywords are matched to classes
and functions containing extra names in their
'extra_keyword_matches' set, as well as functions which
have names assigned directly to them. The matching is
case-insensitive.
-m MARKEXPR Only run tests matching given mark expression. For
example: -m 'mark1 and not mark2'.
--markers show markers (builtin, plugin and per-project ones).
-x, --exitfirst Exit instantly on first error or failed test
--fixtures, --funcargs
Show available fixtures, sorted by plugin appearance
(fixtures with leading '_' are only shown with '-v')
--fixtures-per-test Show fixtures per test
--pdb Start the interactive Python debugger on errors or
KeyboardInterrupt
...
to see available markers type: pytest --markers
to see available fixtures type: pytest --fixtures
(shown according to specified file_or_dir or current dir if not specified; fixtures with leading '_' are only shown with the '-v' option
warning : /usr/local/lib/python3.13/site-packages/_pytest/config/__init__.py:1405: PytestConfigWarning: could not load initial conftests: /tmp/pytest-of-root/pytest-22/test_django_bad_conftest0/conftest.py
self.issue_config_time_warning(
@kingbuzzman I'm also encountering this issue. For me, it's because I have in conftest.py:
from django.contrib.auth.models import User
Indeed, by default it just causes:
warning : .../lib/python3.13/site-packages/_pytest/config/__init__.py:1405: PytestConfigWarning: could not load initial conftests: .../tests/conftest.py
If I include as a Pytest option:
[tool.pytest.ini_options]
filterwarnings = ["error"]
then it becomes an error.
This helps.. but i still cannot reproduce it. Is this an open source project that is having this issue? Can you provide a link? I’m very motivated in getting to the root cause of this.
Sorry the repo I had the issue on isn't open source. Maybe someone else has a shareable example?
@brianhelba can you provide a sample project that replicates this issue?