from django.test import TestCase from django.test.client import Client class ClientTest(TestCase): """ Small test suite to demonstrate helper methods. You'd probably want to abstract these to your own subclass of django.test.TestCase so you could import and use it in each of your tests.py files. """ def setUp(self): """This method is automatically called by the Django test framework.""" self.client = Client() def GET(self, url, status=200, mimetype="text/html"): """Get a URL and require a specific status code before proceeding""" response = self.client.get(url) self.failUnlessEqual(response.status_code, status) self.failUnless(response.headers['Content-Type'].startswith(mimetype)) return response def POST(self, url, params, status=200, mimetype="text/html"): """Make a POST and require a specific status code before proceeding""" response = self.client.post(url, params) self.failUnlessEqual(response.status_code, status) self.failUnless(response.headers['Content-Type'].startswith(mimetype)) return response def test_examples(self): # Here we expect a 200 response, so we don't need to specify self.GET("/") # Here we specify we're looking for a 404 self.GET("/boguspath/", status=404) # Here we're also interested in the mimetype self.GET("/robots.txt", mimetype="text/plain") # Here we test a post and see whether it redirects on success. sample = { 'name': "Bob the Builder", 'slogan': "Yes we can!", } response = self.POST("/", sample, status=302) # We'd also want to inspect the response to see *where* it # redirects to, etc.