Skip to content

Commit

Permalink
Show django settings in pytest output.
Browse files Browse the repository at this point in the history
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/pytest-django#199
for more information.
  • Loading branch information
beyondgeeks committed Oct 5, 2015
1 parent 68ed627 commit c3cc4c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
26 changes: 23 additions & 3 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,24 @@ def pytest_load_initial_conftests(early_config, parser, args):
os.environ[INVALID_TEMPLATE_VARS_ENV] = 'true'

# Configure DJANGO_SETTINGS_MODULE
ds = (options.ds or
os.environ.get(SETTINGS_MODULE_ENV) or
early_config.getini(SETTINGS_MODULE_ENV))

if options.ds:
ds_source = 'command line option'
ds = options.ds
elif SETTINGS_MODULE_ENV in os.environ:
ds = os.environ[SETTINGS_MODULE_ENV]
ds_source = 'environment variable'
elif early_config.getini(SETTINGS_MODULE_ENV):
ds = early_config.getini(SETTINGS_MODULE_ENV)
ds_source = 'ini file'
else:
ds = None
ds_source = None

if ds:
early_config._dsm_report_header = 'django settings: %s (from %s)' % (ds, ds_source)
else:
early_config._dsm_report_header = None

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


def pytest_report_header(config):
if config._dsm_report_header:
return [config._dsm_report_header]


@pytest.mark.trylast
def pytest_configure():
# Allow Django settings to be configured in a user pytest_configure call,
Expand Down
57 changes: 39 additions & 18 deletions tests/test_django_settings_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
'''


def test_ds_ini(testdir, monkeypatch):
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
testdir.makeini("""
[pytest]
DJANGO_SETTINGS_MODULE = tpkg.settings_ini
""")
pkg = testdir.mkpydir('tpkg')
pkg.join('settings_ini.py').write(BARE_SETTINGS)
testdir.makepyfile("""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
""")
result = testdir.runpytest_subprocess()
assert result.parseoutcomes()['passed'] == 1
result.stdout.fnmatch_lines(['django settings: tpkg.settings_ini (from ini file)*'])
assert result.ret == 0


def test_ds_env(testdir, monkeypatch):
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.settings_env')
pkg = testdir.mkpydir('tpkg')
Expand All @@ -31,48 +51,49 @@ def test_settings():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_env'
""")
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(['*1 passed*'])
assert result.ret == 0
result.stdout.fnmatch_lines(['django settings: tpkg.settings_env (from '
'environment variable)*'])
assert result.parseoutcomes()['passed'] == 1


def test_ds_ini(testdir, monkeypatch):
"DSM env should override ini."
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.settings_ini')
testdir.makeini("""\
def test_ds_option(testdir, monkeypatch):
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'DO_NOT_USE_env')
testdir.makeini("""
[pytest]
DJANGO_SETTINGS_MODULE = DO_NOT_USE_ini
""")
pkg = testdir.mkpydir('tpkg')
settings = pkg.join('settings_ini.py')
settings = pkg.join('settings_opt.py')
settings.write(BARE_SETTINGS)
testdir.makepyfile("""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_opt'
""")
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(['*1 passed*'])
assert result.ret == 0
result = testdir.runpytest_subprocess('--ds=tpkg.settings_opt')
result.stdout.fnmatch_lines(['django settings: tpkg.settings_opt (from command line option)'])
assert result.parseoutcomes()['passed'] == 1


def test_ds_option(testdir, monkeypatch):
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'DO_NOT_USE_env')
testdir.makeini("""
def test_ds_env_override_ini(testdir, monkeypatch):
"DSM env should override ini."
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.settings_env')
testdir.makeini("""\
[pytest]
DJANGO_SETTINGS_MODULE = DO_NOT_USE_ini
""")
pkg = testdir.mkpydir('tpkg')
settings = pkg.join('settings_opt.py')
settings = pkg.join('settings_env.py')
settings.write(BARE_SETTINGS)
testdir.makepyfile("""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_opt'
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_env'
""")
result = testdir.runpytest_subprocess('--ds=tpkg.settings_opt')
result.stdout.fnmatch_lines(['*1 passed*'])
result = testdir.runpytest_subprocess()
assert result.parseoutcomes()['passed'] == 1
assert result.ret == 0


Expand Down

0 comments on commit c3cc4c0

Please sign in to comment.