Skip to content

Commit 61372a2

Browse files
committed
Skip Django setup and manage.py scanning with "pytest --help"
With `pytest --version` and `pytest --help` pytest-django should not throw an error (ImportError) in case of invalid DSM settings etc. This removes hooking into `pytest_configure` altogether, and moves the late call do `_setup_django` into the session-scoped autoload fixture instead. Fixes #235
1 parent 1f48ebf commit 61372a2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

pytest_django/plugin.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def pytest_load_initial_conftests(early_config, parser, args):
162162

163163
options = parser.parse_known_args(args)
164164

165+
if options.version or options.help:
166+
return
167+
165168
django_find_project = _parse_django_find_project_ini(
166169
early_config.getini('django_find_project'))
167170

@@ -200,12 +203,6 @@ def pytest_load_initial_conftests(early_config, parser, args):
200203
_setup_django()
201204

202205

203-
@pytest.mark.trylast
204-
def pytest_configure():
205-
if django_settings_is_configured():
206-
_setup_django()
207-
208-
209206
def pytest_runtest_setup(item):
210207

211208
if django_settings_is_configured() and is_django_unittest(item):
@@ -234,6 +231,7 @@ def _django_test_environment(request):
234231
we need to follow this model.
235232
"""
236233
if django_settings_is_configured():
234+
_setup_django()
237235
from django.conf import settings
238236
from .compat import setup_test_environment, teardown_test_environment
239237
settings.DEBUG = False

tests/test_manage_py_scan.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ def test_django_project_scan_disabled_invalid_settings(django_testdir,
5050
result.stderr.fnmatch_lines(['*ImportError*DOES_NOT_EXIST*'])
5151
result.stderr.fnmatch_lines(['*pytest-django did not search for '
5252
'Django projects*'])
53+
54+
55+
@pytest.mark.django_project(project_root='django_project_root',
56+
create_manage_py=True)
57+
def test_django_project_found_invalid_settings_version(django_testdir, monkeypatch):
58+
"""Invalid DSM should not cause an error with --help or --version."""
59+
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'DOES_NOT_EXIST')
60+
61+
result = django_testdir.runpytest('django_project_root', '--version')
62+
assert result.ret == 0
63+
result.stderr.fnmatch_lines(['*This is pytest version*'])
64+
65+
result = django_testdir.runpytest('django_project_root', '--help')
66+
assert result.ret == 0
67+
result.stdout.fnmatch_lines(['*usage:*'])

0 commit comments

Comments
 (0)