Skip to content

Commit fe7d280

Browse files
committed
Merge pull request #238 from blueyed/no-error-for-help-and-version
Skip Django setup and manage.py scanning with "pytest --help". Thanks @blueyed
2 parents adedb06 + 61372a2 commit fe7d280

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
@@ -171,6 +171,9 @@ def pytest_load_initial_conftests(early_config, parser, args):
171171

172172
options = parser.parse_known_args(args)
173173

174+
if options.version or options.help:
175+
return
176+
174177
django_find_project = _parse_django_find_project_ini(
175178
early_config.getini('django_find_project'))
176179

@@ -217,12 +220,6 @@ def pytest_load_initial_conftests(early_config, parser, args):
217220
_setup_django()
218221

219222

220-
@pytest.mark.trylast
221-
def pytest_configure():
222-
if django_settings_is_configured():
223-
_setup_django()
224-
225-
226223
def pytest_runtest_setup(item):
227224

228225
if django_settings_is_configured() and is_django_unittest(item):
@@ -251,6 +248,7 @@ def _django_test_environment(request):
251248
we need to follow this model.
252249
"""
253250
if django_settings_is_configured():
251+
_setup_django()
254252
from django.conf import settings
255253
from .compat import setup_test_environment, teardown_test_environment
256254
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)