Sunday, November 29, 2009

Getting Started with Testing in Django

Following yesterday's post another hotly requested topic was testing in Django. Today I wanted to give a simple overview on how to get started writing tests for your Django applications. Since Django 1.1, Django has automatically provided a tests.py file when you create a new application, that's where we'll start.

For me the first thing I want to test with my applications is, "Do the views work?". This makes sense, the views are what the user sees, they need to at least be in a working state (200 OK response) before anything else can happen (business logic). So the most basic thing you can do to start testing is something like this:

from django.tests import TestCase
class MyTests(TestCase):
def test_views(self):
response = self.client.get("/my/url/")
self.assertEqual(response.status_code, 200)


By just making sure you run this code before you commit something you've already eliminated a bunch of errors, syntax errors in your URLs or views, typos, forgotten imports, etc. The next thing I like to test is making sure that all the branches of my code are covered, the most common place my views have branches is in views that handle forms, one branch for GET and one for POST. So I'll write a test like this:

from django.tests import TestCase
class MyTests(TestCase):
def test_forms(self):
response = self.client.get("/my/form/")
self.assertEqual(response.status_code, 200)

response = self.client.post("/my/form/", {"data": "value"})
self.assertEqual(response.status_code, 302) # Redirect on form success

response = self.client.post("/my/form/", {})
self.assertEqual(response.status_code, 200) # we get our page back with an error


Now I've tested both the GET and POST conditions on this view, as well the form is valid and form is invalid cases. With this strategy you can have a good base set of tests for any application with not a lot of work. The next step is setting up tests for your business logic. These are a little more complicated, you need to make sure models are created and edited in the right cases, emails are sent in the right places, etc. Django's testing documentation is a great place to read more on writing tests for your applications.

12 comments:

  1. May you continue writing about testing in Django?

    ReplyDelete
  2. Thanks Alex, this was a great introduction to django testing, which is still something I do not touched yet because I never knew were to begin. I would love to read more of about testing in this introductory style. Are there any good resources for this, beside the django documentation?

    ReplyDelete
  3. more more more More More More MORE MORE MORE!!!!

    (please....)


    *cough*
    Sorry, o vacation with the kids and it's rubbing off...

    ReplyDelete
  4. Excellent start! It sure helps me

    ReplyDelete
  5. Thanks! Great advice and very helpful for us "un-testers"! :-)
    I'll start with these simple tests!

    ReplyDelete
  6. I would also like to cast my vote for more posts addressing testing in Django. This was a great introduction. More on business logic testing would be wonderful. Thanks Alex.

    ReplyDelete
  7. I'm finally going to start testing now, thanks Alex.

    ReplyDelete
  8. Just a small correction. It should be:

    from django.test import TestCase

    ReplyDelete
  9. This is a very helpful post I could not figure out how to get started using tests. This has given me a starting point at least! I really appreciate all you do for the Django (and Pinax, and Python) projects. Tom

    ReplyDelete
  10. This is a very great post! Giving me the feeling that Unit tests for Django aren't hard at all and useful at the same time..
    Thanks!

    ReplyDelete
  11. Thanks for the post. Just finished setting up my first set of test & this was a great start.

    ReplyDelete

Note: Only a member of this blog may post a comment.