Skip to main content

Posts

Showing posts with the label unitest

Django: Unittest for HttpResponseRedirect method recipe

I like to invent bicycles in my code. They often come in handy and you basically do not rely on Django version... So upon updating your project's Django version in future times you will not have to refactor half of all the code. So... How do you test http redirects? I usually restrict my time in thinking and write code like if redirect target is hardcoded. And it really is in most of the times. But imagine if you decide to redirect your view response in several places. Or imagine if you're not the only one who owns you'r project's code and there are many collaborators that can change things without warning... And you need to have a test for that view that will test redirects and complex code behavior. I used to check redirects like so: response = self . cliet . get ( '/myview' ) self . assertEqual ( response . status_code , 302 ) # View redirected... All ok... But  if you have some view that can redirect to things that you need to check. How would y...