1

According to the docs of url the_url is empty if the reverse of the url fails:

{% url 'some-url-name' as the_url %}

In my case I would like to get an exception, since I want my tests to fail if my code is broken.

How to store the result of reverse to a variable (and get an exception if the reverse failed)?

guettli
  • 25,042
  • 81
  • 346
  • 663

1 Answers1

0

Depends on what you really want to test...

If you only want to test whether "'some-url-name'" can be reversed (regardless of how it's used in templates), the {% url %} tag is mainly a wrapper around django.core.urlresolvers.reverse so that's easy to unittest.

If you want to test that your urls are correctly reversed in your templates, well, the solution is simple: render the template and parse the html to check if the url is ok ;-)

NB: Django has some doc on the topic and a couple useful tools

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118