Skip to content

Fix --help/--version when conftest imports models#1275

Open
brianhelba wants to merge 1 commit intopytest-dev:mainfrom
brianhelba:help-conftest
Open

Fix --help/--version when conftest imports models#1275
brianhelba wants to merge 1 commit intopytest-dev:mainfrom
brianhelba:help-conftest

Conversation

@brianhelba
Copy link

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

@brianhelba
Copy link
Author

@kingbuzzman I was able to fix this myself (and add a test to reproduce the failure). Could you please take a look?

Previously, `pytest_load_initial_conftests` returned early when `--help` or
`--version` was passed (added by pytest-dev#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 pytest-dev#1152
brianhelba added a commit to kitware-resonant/django-auth-style that referenced this pull request Mar 7, 2026
brianhelba added a commit to kitware-resonant/django-auth-style that referenced this pull request Mar 9, 2026
brianhelba added a commit to kitware-resonant/django-auth-style that referenced this pull request Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant