Skip to content

Commit 4b0526e

Browse files
committed
Show django settings in pytest output.
With the recent change to prefer DJANGO_SETTINGS_MODULE from the environment rather than ini-file, it is nice to show which Django settings is used and why it was choosen. See discussion in pytest-dev#199 for more information.
1 parent 726a63f commit 4b0526e

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

pytest_django/plugin.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,24 @@ def pytest_load_initial_conftests(early_config, parser, args):
194194
os.environ[INVALID_TEMPLATE_VARS_ENV] = 'true'
195195

196196
# Configure DJANGO_SETTINGS_MODULE
197-
ds = (options.ds or
198-
os.environ.get(SETTINGS_MODULE_ENV) or
199-
early_config.getini(SETTINGS_MODULE_ENV))
197+
198+
if options.ds:
199+
ds_source = 'command line option'
200+
ds = options.ds
201+
elif SETTINGS_MODULE_ENV in os.environ:
202+
ds = os.environ[SETTINGS_MODULE_ENV]
203+
ds_source = 'environment variable'
204+
elif early_config.getini(SETTINGS_MODULE_ENV):
205+
ds = early_config.getini(SETTINGS_MODULE_ENV)
206+
ds_source = 'ini file'
207+
else:
208+
ds = None
209+
ds_source = None
210+
211+
if ds:
212+
early_config._dsm_report_header = 'django settings: %s (from %s)' % (ds, ds_source)
213+
else:
214+
early_config._dsm_report_header = None
200215

201216
# Configure DJANGO_CONFIGURATION
202217
dc = (options.dc or
@@ -223,6 +238,11 @@ def pytest_load_initial_conftests(early_config, parser, args):
223238
_setup_django()
224239

225240

241+
def pytest_report_header(config):
242+
if config._dsm_report_header:
243+
return [config._dsm_report_header]
244+
245+
226246
@pytest.mark.trylast
227247
def pytest_configure():
228248
# Allow Django settings to be configured in a user pytest_configure call,

tests/test_django_settings_module.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919
'''
2020

2121

22+
def test_ds_ini(testdir, monkeypatch):
23+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
24+
testdir.makeini("""
25+
[pytest]
26+
DJANGO_SETTINGS_MODULE = tpkg.settings_ini
27+
""")
28+
pkg = testdir.mkpydir('tpkg')
29+
pkg.join('settings_ini.py').write(BARE_SETTINGS)
30+
testdir.makepyfile("""
31+
import os
32+
33+
def test_ds():
34+
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
35+
""")
36+
result = testdir.runpytest_subprocess()
37+
assert result.parseoutcomes()['passed'] == 1
38+
result.stdout.fnmatch_lines(['django settings: tpkg.settings_ini (from ini file)*'])
39+
assert result.ret == 0
40+
41+
2242
def test_ds_env(testdir, monkeypatch):
2343
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.settings_env')
2444
pkg = testdir.mkpydir('tpkg')
@@ -31,48 +51,49 @@ def test_settings():
3151
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_env'
3252
""")
3353
result = testdir.runpytest_subprocess()
34-
result.stdout.fnmatch_lines(['*1 passed*'])
35-
assert result.ret == 0
54+
result.stdout.fnmatch_lines(['django settings: tpkg.settings_env (from '
55+
'environment variable)*'])
56+
assert result.parseoutcomes()['passed'] == 1
3657

3758

38-
def test_ds_ini(testdir, monkeypatch):
39-
"DSM env should override ini."
40-
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.settings_ini')
41-
testdir.makeini("""\
59+
def test_ds_option(testdir, monkeypatch):
60+
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'DO_NOT_USE_env')
61+
testdir.makeini("""
4262
[pytest]
4363
DJANGO_SETTINGS_MODULE = DO_NOT_USE_ini
4464
""")
4565
pkg = testdir.mkpydir('tpkg')
46-
settings = pkg.join('settings_ini.py')
66+
settings = pkg.join('settings_opt.py')
4767
settings.write(BARE_SETTINGS)
4868
testdir.makepyfile("""
4969
import os
5070
5171
def test_ds():
52-
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
72+
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_opt'
5373
""")
54-
result = testdir.runpytest_subprocess()
55-
result.stdout.fnmatch_lines(['*1 passed*'])
56-
assert result.ret == 0
74+
result = testdir.runpytest_subprocess('--ds=tpkg.settings_opt')
75+
result.stdout.fnmatch_lines(['django settings: tpkg.settings_opt (from command line option)'])
76+
assert result.parseoutcomes()['passed'] == 1
5777

5878

59-
def test_ds_option(testdir, monkeypatch):
60-
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'DO_NOT_USE_env')
61-
testdir.makeini("""
79+
def test_ds_env_override_ini(testdir, monkeypatch):
80+
"DSM env should override ini."
81+
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.settings_env')
82+
testdir.makeini("""\
6283
[pytest]
6384
DJANGO_SETTINGS_MODULE = DO_NOT_USE_ini
6485
""")
6586
pkg = testdir.mkpydir('tpkg')
66-
settings = pkg.join('settings_opt.py')
87+
settings = pkg.join('settings_env.py')
6788
settings.write(BARE_SETTINGS)
6889
testdir.makepyfile("""
6990
import os
7091
7192
def test_ds():
72-
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_opt'
93+
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_env'
7394
""")
74-
result = testdir.runpytest_subprocess('--ds=tpkg.settings_opt')
75-
result.stdout.fnmatch_lines(['*1 passed*'])
95+
result = testdir.runpytest_subprocess()
96+
assert result.parseoutcomes()['passed'] == 1
7697
assert result.ret == 0
7798

7899

0 commit comments

Comments
 (0)