diff --git a/docs/changelog.rst b/docs/changelog.rst index b1a972e86..54f198dc2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,5 +1,12 @@ Changelog -========= + +Unreleased +---------- + +Features +^^^^^^^^ + +* Add a new option `--django-debug` to specify the default DEBUG setting (#228) 3.4.8 (2019-02-26) ------------------ diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 21f4debf9..d5fbad2c6 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -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`` command line option:: + + $ pytest --django-debug True|False|None + +``None`` do not override the default +``True`` override ``DEBUG`` to be ``True`` +``False`` override ``DEBUG`` to be ``False`` diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 09c61c801..9b9658d22 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -5,6 +5,7 @@ """ import contextlib +from distutils.util import strtobool import inspect from functools import reduce import os @@ -106,6 +107,13 @@ def pytest_addoption(parser): default=False, help="Enable Django migrations on test setup", ) + group._addoption( + "--django-debug", + action="store", + dest="django_debug", + default="None", + help="Configure Django's DEBUG setting." + ) parser.addini( CONFIGURATION_ENV, "django-configurations class to use by pytest-django." ) @@ -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 = request.config.getvalue("django_debug") + if django_debug != "None": + import django + debug = bool(strtobool(django_debug)) + 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) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 09b8427e8..3a970f353 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -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(): @@ -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=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=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=True') assert r.ret == 0