I'm currently running some Django tests and it looks that DEBUG=False
by default. Is there a way to run a specific test where I can set DEBUG=True
at the command line or in code?

- 45,304
- 42
- 117
- 144
-
1According to https://docs.djangoproject.com/en/1.3/topics/testing/#other-test-conditions, test set `DEBUG=False`. I was looking for a way to bypass that. Even if I pass `DEBUG=True` in `settings.py`, it will revert to `False` while running tests. – Thierry Lam Sep 16 '11 at 15:42
-
7Never mind, I can set `settings.DEBUG = True` under `setUp` – Thierry Lam Sep 16 '11 at 16:48
-
2"Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False." [docs](https://docs.djangoproject.com/en/4.1/topics/testing/overview/#other-test-conditions) – Matthew Hegarty Sep 02 '22 at 12:21
6 Answers
For a specific test inside a test case, you can use the override_settings decorator:
from django.test.utils import override_settings
from django.conf import settings
class TestSomething(TestCase):
@override_settings(DEBUG=True)
def test_debug(self):
assert settings.DEBUG

- 3,658
- 2
- 24
- 24
-
-
You can also override the setting for the entire test class. See https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.override_settings – Scott Apr 28 '16 at 20:40
-
correct link: https://docs.djangoproject.com/en/4.1/topics/testing/tools/#overriding-settings – Matthew Hegarty Sep 02 '22 at 12:23
-
Doesn't work if `urlpatterns` is constructed based on `settings.DEBUG`. – Serrano Oct 03 '22 at 16:15
Starting with Django 1.11 you can use --debug-mode
to set the DEBUG setting to True prior to running tests.

- 371
- 3
- 4
-
I have a case similar to Abhimanyu's answer (with `urlpatterns`). This actually works, but none of the other answers work. – Serrano Oct 03 '22 at 16:25
The accepted answer didn't work for me. I use Selenium for testing, and setting @override_settings(DEBUG=True)
makes the test browser always display 404
error on every page. And DEBUG=False
does not show exception tracebacks. So I found a workaround.
The idea is to emulate DEBUG=True
behaviour, using custom 500
handler and built-in django 500
error handler.
Add this to myapp.views:
import sys from django import http from django.views.debug import ExceptionReporter def show_server_error(request): """ 500 error handler to show Django default 500 template with nice error information and traceback. Useful in testing, if you can't set DEBUG=True. Templates: `500.html` Context: sys.exc_info() results """ exc_type, exc_value, exc_traceback = sys.exc_info() error = ExceptionReporter(request, exc_type, exc_value, exc_traceback) return http.HttpResponseServerError(error.get_traceback_html())
urls.py:
from django.conf import settings if settings.TESTING_MODE: # enable this handler only for testing, # so that if DEBUG=False and we're not testing, # the default handler is used handler500 = 'myapp.views.show_server_error'
settings.py:
# detect testing mode import sys TESTING_MODE = 'test' in sys.argv
Now if any of your Selenium tests encounters 500 error, you'll see a nice error page with traceback and everything. If you run a normal non-testing environment, default 500 handler is used.
Inspired by:

- 1
- 1

- 16,269
- 5
- 73
- 81
Okay let's say you want to write tests for error testcase for which the urls are :-
urls.py
if settings.DEBUG:
urlpatterns += [
url(r'^404/$', page_not_found_view),
url(r'^500/$', my_custom_error_view),
url(r'^400/$', bad_request_view),
url(r'^403/$', permission_denied_view),
]
test_urls.py:-
from django.conf import settings
class ErroCodeUrl(TestCase):
def setUp(self):
settings.DEBUG = True
def test_400_error(self):
response = self.client.get('/400/')
self.assertEqual(response.status_code, 500)
Hope you got some idea!

- 725
- 1
- 6
- 20
-
1@override_settings should be used to "unwind" the change before the next test. – rrauenza Jul 16 '20 at 20:54
-
I tried this and it doesn't work (Django 3.2). Even `override_settings(DEBUG=True)` doesn't work in this case. What does work is the `--debug-mode` flag that was suggested in another answer. – Serrano Oct 03 '22 at 16:20
Nothing worked for me except https://stackoverflow.com/a/1118271/5750078 Use Python 3.7
breakpoint()
method. Works fine on pycharm

- 3,643
- 2
- 32
- 47
-
This question is about how to set/ override a specific Django setting - in this case DEBUG - when running unit tests. Your qnswer and the link to the discussion you provided is about debugging. – Ali Apr 23 '21 at 19:09
You can't see the results of DEBUG=True
when running a unit test. The pages don't display anywhere. No browser.
Changing DEBUG
has no effect, since the web pages (with the debugging output) are not visible anywhere.
If you want to see a debugging web page related to a failing unit test, then do this.
Drop your development database.
Rerun
syncdb
to build an empty development database.Run the various
loaddata
scripts to rebuild the fixtures for that test in your development database.Run the server and browse the page.
Now you can see the debug output.

- 384,516
- 81
- 508
- 779
-
1I realize that this is an older post, but a good reason to want `DEBUG=True` is to see messages from the tastypie REST api, which only show if both `DEBUG=True` and `TASTYPIE_FULL_DEBUG=True`. – Tim Feb 07 '13 at 23:11
-
2Also when using selenium you do have the web pages displayed. One may want to see actual error messages should something not work right while building your tests. – Ron E Feb 09 '13 at 21:58
-
2Setting DEBUG=True also allows you to print generated SQL from ``django.db.connection.queries``, which can be helpful in crafting tests to cover strange edge cases. – CJ Gaconnet Jul 19 '13 at 20:58