Skip to content

Commit a074460

Browse files
committed
Implement the --django-debug plugin option with tests
1 parent 17cdcda commit a074460

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

docs/changelog.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
Changelog
2-
=========
2+
3+
Unreleased
4+
----------
5+
6+
Bugfixes
7+
^^^^^^^^
8+
9+
* Added a new option `--django-debug` to specify the default DEBUG setting (#228)
310

411
3.4.8 (2019-02-26)
512
------------------

docs/configuring_django.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,17 @@ itself::
104104
project.app.signals.something = noop
105105

106106
This plugin can then be used e.g. via ``-p`` in :pytest-confval:`addopts`.
107+
108+
``DEBUG`` setting during the test run
109+
-------------------------------------
110+
111+
By default, django test runner behavior is to force DEBUG setting to False. So does the ``pytest-django``.
112+
But sometimes, especially for functional tests, you might want to avoid this, to debug why certain page does not work.
113+
114+
Command Line Option::
115+
116+
$ py.test --django-debug True|False|None
117+
118+
``None`` ensure there is no override of the test settings DEBUG value
119+
``True`` override DEBUG to True
120+
``False`` override DEBUG to False

pytest_django/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import contextlib
8+
from distutils.util import strtobool
89
import inspect
910
from functools import reduce
1011
import os
@@ -106,6 +107,13 @@ def pytest_addoption(parser):
106107
default=False,
107108
help="Enable Django migrations on test setup",
108109
)
110+
group._addoption(
111+
"--django-debug",
112+
action="store",
113+
dest="djangodebug",
114+
default="None",
115+
help="Configure the DEBUG setting. Defaults to False"
116+
)
109117
parser.addini(
110118
CONFIGURATION_ENV, "django-configurations class to use by pytest-django."
111119
)
@@ -466,7 +474,9 @@ def django_test_environment(request):
466474
from django.conf import settings as dj_settings
467475
from django.test.utils import setup_test_environment, teardown_test_environment
468476

469-
dj_settings.DEBUG = False
477+
if request.config.getvalue('djangodebug') != 'None':
478+
dj_settings.DEBUG = bool(strtobool(request.config.getvalue('djangodebug')))
479+
470480
setup_test_environment()
471481
request.addfinalizer(teardown_test_environment)
472482

tests/test_django_settings_module.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,81 @@ def test_debug_is_false():
308308
assert r.ret == 0
309309

310310

311+
def test_no_debug_override(testdir, monkeypatch):
312+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
313+
testdir.makeconftest("""
314+
from django.conf import settings
315+
316+
def pytest_configure():
317+
settings.configure(SECRET_KEY='set from pytest_configure',
318+
DEBUG=True,
319+
DATABASES={'default': {
320+
'ENGINE': 'django.db.backends.sqlite3',
321+
'NAME': ':memory:'}},
322+
INSTALLED_APPS=['django.contrib.auth',
323+
'django.contrib.contenttypes',])
324+
""")
325+
326+
testdir.makepyfile("""
327+
from django.conf import settings
328+
def test_debug_is_unchanged():
329+
assert settings.DEBUG is True
330+
""")
331+
332+
r = testdir.runpytest_subprocess('--django-debug=None')
333+
assert r.ret == 0
334+
335+
336+
def test_override_debug_to_false(testdir, monkeypatch):
337+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
338+
testdir.makeconftest("""
339+
from django.conf import settings
340+
341+
def pytest_configure():
342+
settings.configure(SECRET_KEY='set from pytest_configure',
343+
DEBUG=True,
344+
DATABASES={'default': {
345+
'ENGINE': 'django.db.backends.sqlite3',
346+
'NAME': ':memory:'}},
347+
INSTALLED_APPS=['django.contrib.auth',
348+
'django.contrib.contenttypes',])
349+
""")
350+
351+
testdir.makepyfile("""
352+
from django.conf import settings
353+
def test_debug_is_false():
354+
assert settings.DEBUG is False
355+
""")
356+
357+
r = testdir.runpytest_subprocess('--django-debug=False')
358+
assert r.ret == 0
359+
360+
361+
def test_override_debug_to_true(testdir, monkeypatch):
362+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
363+
testdir.makeconftest("""
364+
from django.conf import settings
365+
366+
def pytest_configure():
367+
settings.configure(SECRET_KEY='set from pytest_configure',
368+
DEBUG=True,
369+
DATABASES={'default': {
370+
'ENGINE': 'django.db.backends.sqlite3',
371+
'NAME': ':memory:'}},
372+
INSTALLED_APPS=['django.contrib.auth',
373+
'django.contrib.contenttypes',])
374+
""")
375+
376+
testdir.makepyfile("""
377+
from django.conf import settings
378+
def test_debug_is_true():
379+
assert settings.DEBUG is True
380+
""")
381+
382+
r = testdir.runpytest_subprocess('--django-debug=True')
383+
assert r.ret == 0
384+
385+
311386
@pytest.mark.skipif(
312387
not hasattr(django, "setup"),
313388
reason="This Django version does not support app loading",

0 commit comments

Comments
 (0)