[Fixed]-Django Testing – Hard code URLs or Not

6πŸ‘

βœ…

I’ve recently started using Twill via django-test-utils to unit test some of my Django work.

Instead of hardcoding URLs and/or using reverse I use things like twill.follow('Blog') (to follow a β€œBlog” link on the page).

This lets you really test the behavior of your website, just like a web browser would see it, and can catch things the other methods can’t. For example, it would fail if you accidentally removed the β€œBlog” link from your navigation links.

πŸ‘€Steve Losh

10πŸ‘

I would recommend to use β€œOption A. reverse()” because it enables you to decouple your test from the location at which the view is mounted.

for example if β€˜/blog/test-blog/’ becomes β€˜/blog/test-better-url-blog/’ for test will still be pertinent.

πŸ‘€yml

1πŸ‘

It is better to use the reverse function to get the urls by view names. This will not only test your views but also ensures that your view names keep the same.

See it as internal API testing. You would recognize if some of your URLs are broken and get reminded to update your {% url %} tags in the templates.

0πŸ‘

Why not do both twill.follow('Blog') & reverse()?

πŸ‘€citadelgrad

Leave a comment