Skip to content

Commit 0e01f05

Browse files
committed
Call django.setup() after user pytest_configure().
Also, call django.setup() early if Django settings is already configured externally.
1 parent 75e5634 commit 0e01f05

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

pytest_django/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def _add_django_project_to_path(args):
125125

126126

127127
def _setup_django():
128+
if 'django' not in sys.modules:
129+
return
130+
128131
import django
129132

130133
if hasattr(django, 'setup'):
@@ -217,7 +220,14 @@ def pytest_load_initial_conftests(early_config, parser, args):
217220
with _handle_import_error(_django_project_scan_outcome):
218221
settings.DATABASES
219222

220-
_setup_django()
223+
_setup_django()
224+
225+
226+
@pytest.mark.trylast
227+
def pytest_configure():
228+
# Allow Django settings to be configured in a user pytest_configure call,
229+
# but make sure we call django.setup()
230+
_setup_django()
221231

222232

223233
def pytest_runtest_setup(item):

tests/test_django_settings_module.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ def test_django_settings_configure(testdir, monkeypatch):
132132
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
133133

134134
p = testdir.makepyfile(run="""
135-
import django
136135
from django.conf import settings
137136
settings.configure(SECRET_KEY='set from settings.configure()',
138137
DATABASES={'default': {
@@ -142,9 +141,6 @@ def test_django_settings_configure(testdir, monkeypatch):
142141
INSTALLED_APPS=['django.contrib.auth',
143142
'django.contrib.contenttypes',])
144143
145-
if hasattr(django, 'setup'):
146-
django.setup()
147-
148144
import pytest
149145
150146
pytest.main()

0 commit comments

Comments
 (0)