Skip to content

Commit fdf4174

Browse files
committed
Add django_debug_mode to configure how DEBUG is set in tests
Fixes #846.
1 parent 65a2525 commit fdf4174

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

docs/usage.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ Additional command line options
2929
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3030
Fail tests that render templates which make use of invalid template variables.
3131

32+
Additional pytest.ini settings
33+
------------------------------
34+
35+
``django_debug_mode`` - change how DEBUG is set
36+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+
38+
By default tests run with the
39+
`DEBUG <https://docs.djangoproject.com/en/stable/ref/settings/#debug>`_
40+
setting set to ``False``. This is to ensure that the observed output of your
41+
code matches what will be seen in a production setting.
42+
43+
If you want ``DEBUG`` to be set::
44+
45+
[pytest]
46+
django_debug_mode = true
47+
48+
You can also use ``django_debug_mode = keep`` to disable the overriding and use
49+
whatever is already set in the Django settings.
50+
3251
Running tests in parallel with pytest-xdist
3352
-------------------------------------------
3453
pytest-django supports running tests on multiple processes to speed up test

pytest_django/plugin.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def pytest_addoption(parser):
118118
type="bool",
119119
default=True,
120120
)
121+
parser.addini(
122+
"django_debug_mode",
123+
"How to set the Django DEBUG setting (default `False`). "
124+
"Use `keep` to not override.",
125+
default="False",
126+
)
121127
group.addoption(
122128
"--fail-on-template-vars",
123129
action="store_true",
@@ -447,7 +453,13 @@ def django_test_environment(request):
447453
_setup_django()
448454
from django.test.utils import setup_test_environment, teardown_test_environment
449455

450-
setup_test_environment(debug=False)
456+
debug_ini = request.config.getini("django_debug_mode")
457+
if debug_ini == "keep":
458+
debug = None
459+
else:
460+
debug = _get_boolean_value(debug_ini, False)
461+
462+
setup_test_environment(debug=debug)
451463
request.addfinalizer(teardown_test_environment)
452464

453465

tests/test_django_settings_module.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def test_settings():
278278
assert result.ret == 0
279279

280280

281-
def test_debug_false(testdir, monkeypatch):
281+
def test_debug_false_by_default(testdir, monkeypatch):
282282
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
283283
testdir.makeconftest(
284284
"""
@@ -307,6 +307,78 @@ def test_debug_is_false():
307307
assert r.ret == 0
308308

309309

310+
@pytest.mark.parametrize('django_debug_mode', (False, True))
311+
def test_django_debug_mode_true_false(testdir, monkeypatch, django_debug_mode):
312+
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
313+
testdir.makeini(
314+
"""
315+
[pytest]
316+
django_debug_mode = {}
317+
""".format(django_debug_mode)
318+
)
319+
testdir.makeconftest(
320+
"""
321+
from django.conf import settings
322+
323+
def pytest_configure():
324+
settings.configure(SECRET_KEY='set from pytest_configure',
325+
DEBUG=%s,
326+
DATABASES={'default': {
327+
'ENGINE': 'django.db.backends.sqlite3',
328+
'NAME': ':memory:'}},
329+
INSTALLED_APPS=['django.contrib.auth',
330+
'django.contrib.contenttypes',])
331+
""" % (not django_debug_mode)
332+
)
333+
334+
testdir.makepyfile(
335+
"""
336+
from django.conf import settings
337+
def test_debug_is_false():
338+
assert settings.DEBUG is {}
339+
""".format(django_debug_mode)
340+
)
341+
342+
r = testdir.runpytest_subprocess()
343+
assert r.ret == 0
344+
345+
346+
@pytest.mark.parametrize('settings_debug', (False, True))
347+
def test_django_debug_mode_keep(testdir, monkeypatch, settings_debug):
348+
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
349+
testdir.makeini(
350+
"""
351+
[pytest]
352+
django_debug_mode = keep
353+
"""
354+
)
355+
testdir.makeconftest(
356+
"""
357+
from django.conf import settings
358+
359+
def pytest_configure():
360+
settings.configure(SECRET_KEY='set from pytest_configure',
361+
DEBUG=%s,
362+
DATABASES={'default': {
363+
'ENGINE': 'django.db.backends.sqlite3',
364+
'NAME': ':memory:'}},
365+
INSTALLED_APPS=['django.contrib.auth',
366+
'django.contrib.contenttypes',])
367+
""" % settings_debug
368+
)
369+
370+
testdir.makepyfile(
371+
"""
372+
from django.conf import settings
373+
def test_debug_is_false():
374+
assert settings.DEBUG is {}
375+
""".format(settings_debug)
376+
)
377+
378+
r = testdir.runpytest_subprocess()
379+
assert r.ret == 0
380+
381+
310382
@pytest.mark.django_project(
311383
extra_settings="""
312384
INSTALLED_APPS = [

0 commit comments

Comments
 (0)