diff --git a/docs/usage.rst b/docs/usage.rst index 58c7e56ba..b15fc15cc 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -29,6 +29,25 @@ Additional command line options ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fail tests that render templates which make use of invalid template variables. +Additional pytest.ini settings +------------------------------ + +``django_debug_mode`` - change how DEBUG is set +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default tests run with the +`DEBUG `_ +setting set to ``False``. This is to ensure that the observed output of your +code matches what will be seen in a production setting. + +If you want ``DEBUG`` to be set:: + + [pytest] + django_debug_mode = true + +You can also use ``django_debug_mode = keep`` to disable the overriding and use +whatever is already set in the Django settings. + Running tests in parallel with pytest-xdist ------------------------------------------- pytest-django supports running tests on multiple processes to speed up test diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index a13b2b81a..a7045cee5 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -118,6 +118,12 @@ def pytest_addoption(parser): type="bool", default=True, ) + parser.addini( + "django_debug_mode", + "How to set the Django DEBUG setting (default `False`). " + "Use `keep` to not override.", + default="False", + ) group.addoption( "--fail-on-template-vars", action="store_true", @@ -445,11 +451,15 @@ def django_test_environment(request): """ if django_settings_is_configured(): _setup_django() - from django.conf import settings as dj_settings from django.test.utils import setup_test_environment, teardown_test_environment - dj_settings.DEBUG = False - setup_test_environment() + debug_ini = request.config.getini("django_debug_mode") + if debug_ini == "keep": + debug = None + else: + debug = _get_boolean_value(debug_ini, False) + + setup_test_environment(debug=debug) request.addfinalizer(teardown_test_environment) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index da16ff543..9040b522e 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -278,7 +278,7 @@ def test_settings(): assert result.ret == 0 -def test_debug_false(testdir, monkeypatch): +def test_debug_false_by_default(testdir, monkeypatch): monkeypatch.delenv("DJANGO_SETTINGS_MODULE") testdir.makeconftest( """ @@ -307,6 +307,78 @@ def test_debug_is_false(): assert r.ret == 0 +@pytest.mark.parametrize('django_debug_mode', (False, True)) +def test_django_debug_mode_true_false(testdir, monkeypatch, django_debug_mode): + monkeypatch.delenv("DJANGO_SETTINGS_MODULE") + testdir.makeini( + """ + [pytest] + django_debug_mode = {} + """.format(django_debug_mode) + ) + testdir.makeconftest( + """ + from django.conf import settings + + def pytest_configure(): + settings.configure(SECRET_KEY='set from pytest_configure', + DEBUG=%s, + DATABASES={'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:'}}, + INSTALLED_APPS=['django.contrib.auth', + 'django.contrib.contenttypes',]) + """ % (not django_debug_mode) + ) + + testdir.makepyfile( + """ + from django.conf import settings + def test_debug_is_false(): + assert settings.DEBUG is {} + """.format(django_debug_mode) + ) + + r = testdir.runpytest_subprocess() + assert r.ret == 0 + + +@pytest.mark.parametrize('settings_debug', (False, True)) +def test_django_debug_mode_keep(testdir, monkeypatch, settings_debug): + monkeypatch.delenv("DJANGO_SETTINGS_MODULE") + testdir.makeini( + """ + [pytest] + django_debug_mode = keep + """ + ) + testdir.makeconftest( + """ + from django.conf import settings + + def pytest_configure(): + settings.configure(SECRET_KEY='set from pytest_configure', + DEBUG=%s, + DATABASES={'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:'}}, + INSTALLED_APPS=['django.contrib.auth', + 'django.contrib.contenttypes',]) + """ % settings_debug + ) + + testdir.makepyfile( + """ + from django.conf import settings + def test_debug_is_false(): + assert settings.DEBUG is {} + """.format(settings_debug) + ) + + r = testdir.runpytest_subprocess() + assert r.ret == 0 + + @pytest.mark.django_project( extra_settings=""" INSTALLED_APPS = [