diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 89161d5..bffca27 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -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 @@ -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, diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 4a1f25e..593acfc 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -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') @@ -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