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.
May you continue writing about testing in Django?
ReplyDeleteThanks 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?
ReplyDeletemore more more More More More MORE MORE MORE!!!!
ReplyDelete(please....)
*cough*
Sorry, o vacation with the kids and it's rubbing off...
Excellent start! It sure helps me
ReplyDeleteThanks! Great advice and very helpful for us "un-testers"! :-)
ReplyDeleteI'll start with these simple tests!
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.
ReplyDeleteI'm finally going to start testing now, thanks Alex.
ReplyDeleteJust a small correction. It should be:
ReplyDeletefrom django.test import TestCase
Great post!
ReplyDelete+1 More please!
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
ReplyDeleteThis 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..
ReplyDeleteThanks!
Thanks for the post. Just finished setting up my first set of test & this was a great start.
ReplyDelete