Skip to content

Commit 75f89a6

Browse files
committed
Implement the --django-debug plugin option with tests
1 parent 94cccb9 commit 75f89a6

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

docs/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
=========
33

4+
Unreleased
5+
----------
6+
7+
Features
8+
^^^^^^^^
9+
10+
* Added a new option `--django-debug` to specify the default DEBUG setting (#228)
11+
412
3.1.2
513
-----
614

docs/configuring_django.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,17 @@ This can be done from your project's ``conftest.py`` file::
8383
def pytest_configure():
8484
settings.configure(DATABASES=...)
8585

86+
87+
``DEBUG`` setting during the test run
88+
-------------------------------------
89+
90+
By default, django test runner behavior is to force DEBUG setting to False. So does the ``pytest-django``.
91+
But sometimes, especially for functional tests, you might want to avoid this, to debug why certain page does not work.
92+
93+
Command Line Option::
94+
95+
$ py.test --django-debug True|False|None
96+
97+
``None`` ensure there is no override of the test settings DEBUG value
98+
``True`` override DEBUG to True
99+
``False`` override DEBUG to False

pytest_django/plugin.py

Lines changed: 6 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
@@ -66,6 +67,9 @@ def pytest_addoption(parser):
6667
group._addoption('--migrations',
6768
action='store_false', dest='nomigrations', default=False,
6869
help='Enable Django migrations on test setup')
70+
group._addoption('--django-debug',
71+
action='store', dest='djangodebug', default='False',
72+
help='Configure the DEBUG setting. Defaults to False')
6973
parser.addini(CONFIGURATION_ENV,
7074
'django-configurations class to use by pytest-django.')
7175
group._addoption('--liveserver', default=None,
@@ -335,7 +339,8 @@ def django_test_environment(request):
335339
from django.conf import settings as dj_settings
336340
from django.test.utils import (setup_test_environment,
337341
teardown_test_environment)
338-
dj_settings.DEBUG = False
342+
if request.config.getvalue('djangodebug') != 'None':
343+
dj_settings.DEBUG = bool(strtobool(request.config.getvalue('djangodebug')))
339344
setup_test_environment()
340345
request.addfinalizer(teardown_test_environment)
341346

tests/test_django_settings_module.py

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

277277

278+
def test_no_debug_override(testdir, monkeypatch):
279+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
280+
testdir.makeconftest("""
281+
from django.conf import settings
282+
283+
def pytest_configure():
284+
settings.configure(SECRET_KEY='set from pytest_configure',
285+
DEBUG=True,
286+
DATABASES={'default': {
287+
'ENGINE': 'django.db.backends.sqlite3',
288+
'NAME': ':memory:'}},
289+
INSTALLED_APPS=['django.contrib.auth',
290+
'django.contrib.contenttypes',])
291+
""")
292+
293+
testdir.makepyfile("""
294+
from django.conf import settings
295+
def test_debug_is_unchanged():
296+
assert settings.DEBUG is True
297+
""")
298+
299+
r = testdir.runpytest_subprocess('--django-debug=None')
300+
assert r.ret == 0
301+
302+
303+
def test_override_debug_to_false(testdir, monkeypatch):
304+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
305+
testdir.makeconftest("""
306+
from django.conf import settings
307+
308+
def pytest_configure():
309+
settings.configure(SECRET_KEY='set from pytest_configure',
310+
DEBUG=True,
311+
DATABASES={'default': {
312+
'ENGINE': 'django.db.backends.sqlite3',
313+
'NAME': ':memory:'}},
314+
INSTALLED_APPS=['django.contrib.auth',
315+
'django.contrib.contenttypes',])
316+
""")
317+
318+
testdir.makepyfile("""
319+
from django.conf import settings
320+
def test_debug_is_false():
321+
assert settings.DEBUG is False
322+
""")
323+
324+
r = testdir.runpytest_subprocess('--django-debug=False')
325+
assert r.ret == 0
326+
327+
328+
def test_override_debug_to_true(testdir, monkeypatch):
329+
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
330+
testdir.makeconftest("""
331+
from django.conf import settings
332+
333+
def pytest_configure():
334+
settings.configure(SECRET_KEY='set from pytest_configure',
335+
DEBUG=True,
336+
DATABASES={'default': {
337+
'ENGINE': 'django.db.backends.sqlite3',
338+
'NAME': ':memory:'}},
339+
INSTALLED_APPS=['django.contrib.auth',
340+
'django.contrib.contenttypes',])
341+
""")
342+
343+
testdir.makepyfile("""
344+
from django.conf import settings
345+
def test_debug_is_true():
346+
assert settings.DEBUG is True
347+
""")
348+
349+
r = testdir.runpytest_subprocess('--django-debug=True')
350+
assert r.ret == 0
351+
352+
278353
@pytest.mark.skipif(not hasattr(django, 'setup'),
279354
reason="This Django version does not support app loading")
280355
@pytest.mark.django_project(extra_settings="""

0 commit comments

Comments
 (0)