Testing Django Views

Majority of web frameworks promote MVC/MTV software pattern. The way web applications are designed today aren’t same as 5-6 years back. Back then it was server side templates, HTML, API’s weren’t widespread and mobile apps were becoming popular. Rise of mobile and Single Page Application shifted majority of web development towards API centric development. Testing API is super simple with data in and data out, but testing a django view in classic web application is difficult since HTML is returned. REST semantics and status code helped to distinguish response without inspecting body.

View is the co ordinator in the web application. Writing integration tests makes sense for views. While writing integration tests most of the assertions are around permission, status code, returned data. Django provides set of nice helpers to test the views like assertTemplateUsed, assertRedirects. The response returned contains the template context. All three are very handy to test majority of the view functionality and super helpful.

Sample test cases

More of these tests can be found in junction.

See also

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Powered by Buttondown.