Repository: pytest-dev/pytest-django (python)

Issue:
Django debug should be optional
https://github.com/pytest-dev/pytest-django/blob/162263f338d863fddc14b0659f506d63799f78e1/pytest_django/plugin.py#L473

Currently django is hard codded to not allow debug.  This means that static files are not served, even when debug is set as true.  So in my tests when loading from staticfiles_storage the files are not found without a collect static.  What is the expected way to deal with this?


Discussion:
I get having a default flag for DEBUG = False. But if in user's settings they explicitly specify DEBUG = True. I don't think the library should override it.
I prefer to run tests with ``DEBUG`` set to ``True``, and I can't seem to do that with pytest-django.

Am I missing something, or if I'm not, is there a workaround?

The benefit of doing this is that when a test fails because of an unhandled exception, the resulting screenshot contains debugging information.
Duplicate of #180.

This is done to match Django's behavior: https://docs.djangoproject.com/en/dev/topics/testing/overview/#other-test-conditions

But seems like Django has since added a [`--debug-mode` flag](https://docs.djangoproject.com/en/3.1/ref/django-admin/#test) to enable DEBUG. So we can probably do the same. I'll look into it now.

Patch:
diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py
--- a/pytest_django/plugin.py
+++ b/pytest_django/plugin.py
@@ -118,6 +118,12 @@ def pytest_addoption(parser):
         type="bool",
         default=True,
     )
+    parser.addini(
+        "django_debug_mode",
+        "How to set the Django DEBUG setting (default `False`). "
+        "Use `keep` to not override.",
+        default="False",
+    )
     group.addoption(
         "--fail-on-template-vars",
         action="store_true",
@@ -445,11 +451,15 @@ 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()
+        debug_ini = request.config.getini("django_debug_mode")
+        if debug_ini == "keep":
+            debug = None
+        else:
+            debug = _get_boolean_value(debug_ini, False)
+
+        setup_test_environment(debug=debug)
         request.addfinalizer(teardown_test_environment)
 
 


Test patch:
diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py
--- a/tests/test_django_settings_module.py
+++ b/tests/test_django_settings_module.py
@@ -278,7 +278,7 @@ def test_settings():
     assert result.ret == 0
 
 
-def test_debug_false(testdir, monkeypatch):
+def test_debug_false_by_default(testdir, monkeypatch):
     monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
     testdir.makeconftest(
         """
@@ -307,6 +307,78 @@ def test_debug_is_false():
     assert r.ret == 0
 
 
+@pytest.mark.parametrize('django_debug_mode', (False, True))
+def test_django_debug_mode_true_false(testdir, monkeypatch, django_debug_mode):
+    monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
+    testdir.makeini(
+        """
+       [pytest]
+       django_debug_mode = {}
+    """.format(django_debug_mode)
+    )
+    testdir.makeconftest(
+        """
+        from django.conf import settings
+
+        def pytest_configure():
+            settings.configure(SECRET_KEY='set from pytest_configure',
+                               DEBUG=%s,
+                               DATABASES={'default': {
+                                   'ENGINE': 'django.db.backends.sqlite3',
+                                   'NAME': ':memory:'}},
+                               INSTALLED_APPS=['django.contrib.auth',
+                                               'django.contrib.contenttypes',])
+    """ % (not django_debug_mode)
+    )
+
+    testdir.makepyfile(
+        """
+        from django.conf import settings
+        def test_debug_is_false():
+            assert settings.DEBUG is {}
+    """.format(django_debug_mode)
+    )
+
+    r = testdir.runpytest_subprocess()
+    assert r.ret == 0
+
+
+@pytest.mark.parametrize('settings_debug', (False, True))
+def test_django_debug_mode_keep(testdir, monkeypatch, settings_debug):
+    monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
+    testdir.makeini(
+        """
+       [pytest]
+       django_debug_mode = keep
+    """
+    )
+    testdir.makeconftest(
+        """
+        from django.conf import settings
+
+        def pytest_configure():
+            settings.configure(SECRET_KEY='set from pytest_configure',
+                               DEBUG=%s,
+                               DATABASES={'default': {
+                                   'ENGINE': 'django.db.backends.sqlite3',
+                                   'NAME': ':memory:'}},
+                               INSTALLED_APPS=['django.contrib.auth',
+                                               'django.contrib.contenttypes',])
+    """ % settings_debug
+    )
+
+    testdir.makepyfile(
+        """
+        from django.conf import settings
+        def test_debug_is_false():
+            assert settings.DEBUG is {}
+    """.format(settings_debug)
+    )
+
+    r = testdir.runpytest_subprocess()
+    assert r.ret == 0
+
+
 @pytest.mark.django_project(
     extra_settings="""
     INSTALLED_APPS = [