Index ¦ Archives ¦ Atom ¦ RSS

py.test and Django

Test django application with py.test and WebTest

I quit using default Django test suite some years ago, sometimes I go back to it to see if something new can change my mind, but up to now I continue to prefer my choice:


Several reasons - here's a few:

  • It can run unittest, doctest, and nose, style tests suites, making it ideal for new and legacy projects.
  • It includes an intuitively named raises context manager for testing exceptions.
  • You can define fixture arguments to generate baseline data. This is very, very different from Django-style fixtures.
  • Via pytest.ini/setup.cfg you can change the behavior of pytest.
  • Integrates nicely with

  • WebTest allows you to model a user's experience much more closely as it is smart about mark-up. Instead of hand-crafting GET and POST requests, you can use the WebTest API to follow links and submit forms - this is what users actually do. As a result, your tests accurately capture user stories.

  • Writing functional tests with WebTest is both easier and quicker than using Django's test client. It's much simpler to fill in forms that construct complicated arrays of POST data - this is particularly noticable with formsets.
  • The WebTest response object supports several ways of parsing the response HTML, making it easy to make complicated assertions about the response.
  • response.showbrowser() is simply amazing

© 2014 Stefano Apostolico. Built using Pelican. Member of the Internet Defense League.