Skip to content

Commit 617176a

Browse files
committed
Fix --help/--version when conftest imports models
Previously, `pytest_load_initial_conftests` returned early when `--help` or `--version` was passed (added by #238), skipping `django.setup()` entirely. This caused conftest files with top-level Django model imports to fail with `AppRegistryNotReady`. Instead of returning early, continue with normal Django initialization, allowing `--help`/`--version` to return properly when everything is valid. If Django is somehow invalid (e.g. incorrect `DJANGO_SETTINGS_MODULE`), add a guard to still return gracefully if `--help`/`--version` is requested. Running real tests would still fail, but these basic informational arguments shouldn't be blocked. Fixes #1152
1 parent 3955b18 commit 617176a

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

pytest_django/plugin.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ def pytest_load_initial_conftests(
307307

308308
options = parser.parse_known_args(args)
309309

310-
if options.version or options.help:
311-
return
310+
is_help_or_version = bool(options.version or options.help)
312311

313312
django_find_project = _get_boolean_value(
314313
early_config.getini("django_find_project"), "django_find_project"
@@ -363,11 +362,21 @@ def _get_option_with_source(
363362
from django.conf import settings as dj_settings
364363

365364
with _handle_import_error(_django_project_scan_outcome):
366-
dj_settings.DATABASES # noqa: B018
365+
try:
366+
dj_settings.DATABASES # noqa: B018
367+
except ImportError:
368+
# Don't allow invalid settings to block printing these
369+
if not is_help_or_version:
370+
raise
367371

368372
early_config.stash[blocking_manager_key] = DjangoDbBlocker(_ispytest=True)
369373

370-
_setup_django(early_config)
374+
try:
375+
_setup_django(early_config)
376+
except Exception:
377+
# Don't allow invalid settings to block printing these
378+
if not is_help_or_version:
379+
raise
371380

372381

373382
@pytest.hookimpl(trylast=True)

tests/test_initialization.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from textwrap import dedent
24

35
from .helpers import DjangoPytester
@@ -60,3 +62,38 @@ def test_ds():
6062
]
6163
)
6264
assert result.ret == 0
65+
66+
67+
@pytest.mark.parametrize("flag", ["--help", "--version"])
68+
def test_django_setup_for_help_and_version_with_model_import(
69+
django_pytester: DjangoPytester,
70+
flag: str,
71+
) -> None:
72+
"""Django should be set up before conftest files are loaded, even with
73+
--help/--version, so that top-level model imports in conftest files
74+
don't fail with AppRegistryNotReady.
75+
76+
Regression test for https://github.com/pytest-dev/pytest-django/issues/1152
77+
"""
78+
django_pytester.makeconftest(
79+
"""
80+
from tpkg.app.models import Item
81+
"""
82+
)
83+
84+
django_pytester.makepyfile(
85+
"""
86+
def test_placeholder():
87+
pass
88+
"""
89+
)
90+
91+
args = [flag]
92+
if flag == "--version":
93+
# pytest requires -V passed twice to actually print version info
94+
args.append(flag)
95+
96+
result = django_pytester.runpytest_subprocess(*args)
97+
assert result.ret == 0
98+
result.stderr.no_fnmatch_line("*AppRegistryNotReady*")
99+
result.stderr.no_fnmatch_line("*could not load initial conftests*")

0 commit comments

Comments
 (0)