January 21, 2009
Django testing tip: don’t test template output
When writing tests for Django views, especially for projects at work, I’ve almost completely abandoned any sort of detailed test for the template being rendered.
My tests usually look something like this:
def test_link_archive_should_show_published_links(self): """Links in draft status shouldn't appear in the archive.""" #create some data for testing, optionally use a fixture l = Link(url="http://www.heisel.org/", title="Worst. Blog. Ever.", published=False) l.save() r = self.client.get("/links/archive/") self.assertValidResponse(r) self.assertContains(r, l.title, 0) l.published = True l.save() r = self.client.get("/links/archive/") self.assertValidResponse(r) self.assertContains(r, l.title, 1)
It’s slightly heretical, I know. But you don’t want your test suite breaking because of a change in HTML, or a change in your date format, or the addition of new output, etc.
Those changes are frequently made far down the road from the initial launch (say during a redesign) when you’re likely not to remember why you tested for such fine grained output.
Since I’m generally testing first (and who isn’t?) I use the view test as a chance to figure out what information — no matter the whims of the designer, destiny or the sands of time — really has to be presented to the user for this view to fulfill it’s job.
Test for the presence of those strings and move on.
Filed under: Django,Programming
Next: Adventures in unit testing the Basecamp API
Previous: Django Template Development review
Related
-
http://ericholscher.com Eric Holscher
-
http://heisel.org Chris
-
Karol