How do you set DEBUG to True when running a Django test?

Django

Django Problem Overview


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?

Django Solutions


Solution 1 - Django

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

Solution 2 - Django

Starting with Django 1.11 you can use --debug-mode to set the DEBUG setting to True prior to running tests.

Solution 3 - Django

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.

  1. 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())
    
  2. 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'
    
  3. 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:

Solution 4 - Django

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!

Solution 5 - Django

Nothing worked for me except https://stackoverflow.com/a/1118271/5750078 Use Python 3.7

breakpoint() 

method. Works fine on pycharm

Solution 6 - Django

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.

  1. Drop your development database.

  2. Rerun syncdb to build an empty development database.

  3. Run the various loaddata scripts to rebuild the fixtures for that test in your development database.

  4. Run the server and browse the page.

Now you can see the debug output.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThierry LamView Question on Stackoverflow
Solution 1 - DjangoferrouswheelView Answer on Stackoverflow
Solution 2 - DjangoAna BalicaView Answer on Stackoverflow
Solution 3 - DjangoDennis GolomazovView Answer on Stackoverflow
Solution 4 - DjangoAbhimanyuView Answer on Stackoverflow
Solution 5 - DjangoAlvaro Rodriguez ScelzaView Answer on Stackoverflow
Solution 6 - DjangoS.LottView Answer on Stackoverflow