Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the --django-debug plugin option with tests #463

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/changelog.rst
@@ -1,5 +1,12 @@
Changelog
=========

Unreleased
----------

Features
^^^^^^^^

* Add a new option `--django-debug-mode` to specify the default DEBUG setting (#228)

3.4.8 (2019-02-26)
------------------
Expand Down
13 changes: 13 additions & 0 deletions docs/configuring_django.rst
Expand Up @@ -104,3 +104,16 @@ itself::
project.app.signals.something = noop

This plugin can then be used e.g. via ``-p`` in :pytest-confval:`addopts`.

``DEBUG`` setting during the test run
-------------------------------------

Django's test runner forces ``DEBUG`` to be ``False`` by default, and so
does ``pytest-django``.
This can be controlled using the ``--django-debug-mode`` command line option::

$ pytest --django-debug-mode True|False|None

``None`` do not override the default
``True`` override ``DEBUG`` to be ``True``
``False`` override ``DEBUG`` to be ``False``
24 changes: 21 additions & 3 deletions pytest_django/plugin.py
Expand Up @@ -5,6 +5,7 @@
"""

import contextlib
from distutils.util import strtobool
import inspect
from functools import reduce
import os
Expand Down Expand Up @@ -106,6 +107,13 @@ def pytest_addoption(parser):
default=False,
help="Enable Django migrations on test setup",
)
group._addoption(
"--django-debug-mode",
action="store",
dest="django_debug_mode",
default="None",
help="Configure Django's DEBUG setting."
)
parser.addini(
CONFIGURATION_ENV, "django-configurations class to use by pytest-django."
)
Expand Down Expand Up @@ -463,11 +471,21 @@ 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()
setup_test_environment_kwargs = {}

django_debug_mode = request.config.getvalue("django_debug_mode")
if django_debug_mode != "None":
import django
debug = bool(strtobool(django_debug_mode))
if django.VERSION >= (1, 11):
setup_test_environment_kwargs["debug"] = debug
else:
from django.conf import settings as dj_settings
dj_settings.DEBUG = debug

setup_test_environment(**setup_test_environment_kwargs)
request.addfinalizer(teardown_test_environment)


Expand Down
68 changes: 57 additions & 11 deletions tests/test_django_settings_module.py
Expand Up @@ -279,10 +279,9 @@ def test_settings():
assert result.ret == 0


def test_debug_false(testdir, monkeypatch):
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
testdir.makeconftest(
"""
def test_no_debug_override(testdir, monkeypatch):
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
testdir.makeconftest("""
from django.conf import settings

def pytest_configure():
Expand All @@ -293,18 +292,65 @@ def pytest_configure():
'NAME': ':memory:'}},
INSTALLED_APPS=['django.contrib.auth',
'django.contrib.contenttypes',])
"""
)
""")

testdir.makepyfile(
"""
testdir.makepyfile("""
from django.conf import settings
def test_debug_is_unchanged():
assert settings.DEBUG is True
""")

r = testdir.runpytest_subprocess('--django-debug-mode=None')
assert r.ret == 0


def test_override_debug_to_false(testdir, monkeypatch):
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
testdir.makeconftest("""
from django.conf import settings

def pytest_configure():
settings.configure(SECRET_KEY='set from pytest_configure',
DEBUG=True,
DATABASES={'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'}},
INSTALLED_APPS=['django.contrib.auth',
'django.contrib.contenttypes',])
""")

testdir.makepyfile("""
from django.conf import settings
def test_debug_is_false():
assert settings.DEBUG is False
"""
)
""")

r = testdir.runpytest_subprocess()
r = testdir.runpytest_subprocess('--django-debug-mode=False')
assert r.ret == 0


def test_override_debug_to_true(testdir, monkeypatch):
monkeypatch.delenv('DJANGO_SETTINGS_MODULE')
testdir.makeconftest("""
from django.conf import settings

def pytest_configure():
settings.configure(SECRET_KEY='set from pytest_configure',
DEBUG=False,
DATABASES={'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'}},
INSTALLED_APPS=['django.contrib.auth',
'django.contrib.contenttypes',])
""")

testdir.makepyfile("""
from django.conf import settings
def test_debug_is_true():
assert settings.DEBUG is True
""")

r = testdir.runpytest_subprocess('--django-debug-mode=True')
assert r.ret == 0


Expand Down