Skip to content

Commit

Permalink
Merge pull request #888 from bluetech/debug-mode
Browse files Browse the repository at this point in the history
Add django_debug_mode to configure how DEBUG is set in tests
  • Loading branch information
bluetech authored Oct 17, 2020
2 parents 2e564f5 + fdf4174 commit 76170a8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
19 changes: 19 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.djangoproject.com/en/stable/ref/settings/#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
Expand Down
16 changes: 13 additions & 3 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)


Expand Down
74 changes: 73 additions & 1 deletion tests/test_django_settings_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 76170a8

Please sign in to comment.