diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index d51fc9e38..2ac672433 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -171,6 +171,9 @@ def pytest_load_initial_conftests(early_config, parser, args): options = parser.parse_known_args(args) + if options.version or options.help: + return + django_find_project = _parse_django_find_project_ini( early_config.getini('django_find_project')) @@ -217,12 +220,6 @@ def pytest_load_initial_conftests(early_config, parser, args): _setup_django() -@pytest.mark.trylast -def pytest_configure(): - if django_settings_is_configured(): - _setup_django() - - def pytest_runtest_setup(item): if django_settings_is_configured() and is_django_unittest(item): @@ -251,6 +248,7 @@ def _django_test_environment(request): we need to follow this model. """ if django_settings_is_configured(): + _setup_django() from django.conf import settings from .compat import setup_test_environment, teardown_test_environment settings.DEBUG = False diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index 34c168c94..45ec826d5 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -50,3 +50,18 @@ def test_django_project_scan_disabled_invalid_settings(django_testdir, result.stderr.fnmatch_lines(['*ImportError*DOES_NOT_EXIST*']) result.stderr.fnmatch_lines(['*pytest-django did not search for ' 'Django projects*']) + + +@pytest.mark.django_project(project_root='django_project_root', + create_manage_py=True) +def test_django_project_found_invalid_settings_version(django_testdir, monkeypatch): + """Invalid DSM should not cause an error with --help or --version.""" + monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'DOES_NOT_EXIST') + + result = django_testdir.runpytest('django_project_root', '--version') + assert result.ret == 0 + result.stderr.fnmatch_lines(['*This is pytest version*']) + + result = django_testdir.runpytest('django_project_root', '--help') + assert result.ret == 0 + result.stdout.fnmatch_lines(['*usage:*'])