File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -362,8 +362,15 @@ def _get_option_with_source(
362362
363363@pytest .hookimpl (trylast = True )
364364def pytest_configure (config : pytest .Config ) -> None :
365- # Allow Django settings to be configured in a user pytest_configure call,
366- # but make sure we call django.setup()
365+ if config .getoption ("version" , 0 ) > 0 or config .getoption ("help" , False ):
366+ return
367+
368+ # Normally Django is set up in `pytest_load_initial_conftests`, but we also
369+ # allow users to not set DJANGO_SETTINGS_MODULE/`--ds` and instead
370+ # configure the Django settings in a `pytest_configure` hookimpl using e.g.
371+ # `settings.configure(...)`. In this case, the `_setup_django` call in
372+ # `pytest_load_initial_conftests` only partially initializes Django, and
373+ # it's fully initialized here.
367374 _setup_django (config )
368375
369376
Original file line number Diff line number Diff line change @@ -144,6 +144,37 @@ def test_django_project_found_invalid_settings_version(
144144 result .stdout .fnmatch_lines (["*usage:*" ])
145145
146146
147+ @pytest .mark .django_project (project_root = "django_project_root" , create_manage_py = True )
148+ def test_django_project_late_settings_version (
149+ django_pytester : DjangoPytester ,
150+ monkeypatch : pytest .MonkeyPatch ,
151+ ) -> None :
152+ """Late configuration should not cause an error with --help or --version."""
153+ monkeypatch .delenv ("DJANGO_SETTINGS_MODULE" )
154+ django_pytester .makepyfile (
155+ t = "WAT = 1" ,
156+ )
157+ django_pytester .makeconftest (
158+ """
159+ import os
160+
161+ def pytest_configure():
162+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 't')
163+ from django.conf import settings
164+ settings.WAT
165+ """
166+ )
167+
168+ result = django_pytester .runpytest_subprocess ("django_project_root" , "--version" , "--version" )
169+ assert result .ret == 0
170+
171+ result .stdout .fnmatch_lines (["*This is pytest version*" ])
172+
173+ result = django_pytester .runpytest_subprocess ("django_project_root" , "--help" )
174+ assert result .ret == 0
175+ result .stdout .fnmatch_lines (["*usage:*" ])
176+
177+
147178@pytest .mark .django_project (project_root = "django_project_root" , create_manage_py = True )
148179def test_runs_without_error_on_long_args (django_pytester : DjangoPytester ) -> None :
149180 django_pytester .create_test_module (
You can’t perform that action at this time.
0 commit comments