diff --git a/docs/changelog.rst b/docs/changelog.rst index b9dcf20de..2531b4bd7 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,14 @@ Changelog ========= +Unreleased +---------- + +Features +^^^^^^^^ +* Added a new option `--django-debug` to set the DEBUG setting to True prior to + running tests. + 3.1.2 ----- diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index e43dd8688..ef3d696d7 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -83,3 +83,15 @@ This can be done from your project's ``conftest.py`` file:: def pytest_configure(): settings.configure(DATABASES=...) +``DEBUG`` setting during the test run +------------------------------------- + +By default, django test runner forces `DEBUG` setting to `False`, and so does +the ``pytest-django``. But sometimes, especially for functional tests, there is +a need to set `DEBUG` to `True` to investigate why certain page does not work. +To do so, set --django-debug flag in command line. + +Command Line Option:: + + $ pytest --django-debug + diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 1585c6983..52876e7b3 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -69,6 +69,9 @@ def pytest_addoption(parser): group._addoption('--migrations', action='store_false', dest='nomigrations', default=False, help='Enable Django migrations on test setup') + group._addoption('--django-debug', + action='store_true', dest='debug_mode', default=False, + help='Set DEBUG to True prior to run tests') parser.addini(CONFIGURATION_ENV, 'django-configurations class to use by pytest-django.') group._addoption('--liveserver', default=None, @@ -343,11 +346,17 @@ def django_test_environment(request): """ if django_settings_is_configured(): _setup_django() + from distutils.version import StrictVersion + import 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() + debug_mode = request.config.getvalue('debug_mode') + if StrictVersion(django.get_version()) >= StrictVersion('1.11'): + setup_test_environment(debug=debug_mode) + else: + dj_settings.DEBUG = debug_mode + setup_test_environment() request.addfinalizer(teardown_test_environment) diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 5934bf285..b9ff45ea0 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -275,6 +275,30 @@ def test_debug_is_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=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_true(): + assert settings.DEBUG is True + """) + + r = testdir.runpytest_subprocess('--django-debug') + assert r.ret == 0 + + @pytest.mark.skipif(not hasattr(django, 'setup'), reason="This Django version does not support app loading") @pytest.mark.django_project(extra_settings="""