1

I'm trying to set up test for some URLS that are set only in debug. They are not set because apparently the value of DEBUG change to False between my setting file and urls.py. I've never encountered this problem before, and I don't remember doing anything particularly fancy involving DEBUG value.

Here's my urls.py :

from django.conf import settings
from my_views import dnfp
print "settings.DEBUG in url: {}".format(settings.DEBUG)
if settings.DEBUG:
    urlpatterns += [url(r'^dnfp/$', dnfp, name="debug_not_found_page"...

Here's my setting file :

DEBUG=True
print "DEBUG at the end of the settings: {}".format(DEBUG)

The content that fail in my test :

 reverse("debug_not_found_page"), 

Here's the output of the test :

DEBUG at the end of the settings: True
settings.DEBUG in url: False
Creating test database for alias 'default'...
.E
(...)
NoReverseMatch: Reverse for 'debug_not_found_page' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

If I change the value myself in urls.py the url is set again and the test works with this urls.py :

from django.conf import settings
from my_views import dnfp
settings.DEBUG = True
if settings.DEBUG:
    urlpatterns += [url(r'^dnfp/$', dnfp, name="debug_not_found_page"...

Any ideas when and why my value for DEBUG is changing between settings and urls ?

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48

2 Answers2

5

From the docs

Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False. This is to ensure that the observed output of your code matches what will be seen in a production setting.

vishes_shell
  • 22,409
  • 6
  • 71
  • 81
  • 2
    Thanks a lot ! For those of you how googled this, the next step is here : http://stackoverflow.com/questions/7447134/how-do-you-set-debug-to-true-when-running-a-django-test – Pierre.Sassoulas Sep 02 '16 at 09:56
1

The problem with your code is you are setting DEBUG = True after this line

   urlpatterns += [url(r'^dnfp/$', dnfp, name="debug_not_found_page"

The reason is that all the URLs are already appended to urlpatterns[] and you are setting it after the appending of URLs and while appending the URL Django actually transfer control to urls.py for syntax validation purpose. That's why you are getting different value in urls.py.

Set value of DEBUG before this line

Try this, hope it will work.

You can use another approach for doing this, create a separate app for all these type of URLs and don't add the app to INSTALLED_APPS on the basis of debug variable.

Sunil Lulla
  • 797
  • 10
  • 18