Testing Django Applications
So I’ve been here at Disqus for a month or so now. The biggest project on my plate had been testing. Because of the breadth of the Disqus software, my testing has to cover several different approaches to the platform.
- unit testing - using plain python unittests from the unittest module.
- view testing - using Django’s test client.
- in-browser testing - using Selenium RC’s Python driver.
The first two are fairly simple and well documented. The in-browser testing, less so.
The first two are easily supported in Django’s test framework. I can write python unittest classes that inherit from unittest.TestCase and Django test classes that inherit from django.test.TestCase and Django handles the rest. A quick python manage.py test is all it takes.
Selenium complicates things.
Since Selenium is scripting an actual browser, it needs a live web server to connect to and run tests against. I could run a webserver in a static location and point all Selenium tests at that server. This option, however, leaves us lacking in a couple of features that the Django test framework has left us accustomed to.
Django tests are run independently. A fresh database is created and optionally loaded from xml dumps between every method of tests in the test suite. As a result, one test has no effect on another. Simplifying cleanup and expected state.
If I run a static web server, I lose this benefit since the web server cannot use the test suite’s test database that gets recreated between every test.
This is a deal breaker. It makes Selenium tests much harder to write and maintain. As anyone who’s done testing knows, the harder tests are to write and maintain, the more likely they are to be ignored.
Enter Django Live Test Server Support
So I want to bring up a live server when starting every Selenium test. Turns out there was an existing Django ticket for this functionality posted two years ago.
So we here at Disqus decided this would a perfect opportunity to give something back to the Django community and help out anyone else setting out to do testing similar to ours. Along those lines, I’ve written a patch that adds the functionality to start and stop a test server in the Django TestCase class. Which we can use as follows:
class SampleLoginTest(TestCase):
def setUp(self):
self.start_test_server('0.0.0.0', 8000)
self.selenium = selenium('localhost', 4444, '*pifirefox', 'http://127.0.0.1:8000')
def tearDown(self):
self.selenium.stop()
self.stop_test_server()
def test_login_fail(self):
self.selenium.open('/')
self.selenium.click('link=Login')
...
And so on.
We’re now using this patch on our internal copy of Django to manage our Selenium tests helping us keep a better eye on keeping everything working as expected.
We thought we’d share. If you’re setting out on writing tests for a Django application give the patch a try and let me know what you think. Questions and comments welcome at the usual place.
Happy testing!
Devin on July 21st 2008 in disqus
-
Simon Law
-
Devin
-
FirstClown
-
Devin
-
Wap Advertising
-
Luke Hoersten
-
Devin
-
Russell Keith-Magee
-
Devin
-
Graham
-
Mike Patch Tool
-
zstock
-
Rob Loach
-
Regis
-
bhalchander
-
enricodesimone
-
Italian Restaurant
-
Vlad Zablotskyy
-
Daniel Ha
-
Vlad Zablotskyy
-
Daniel Ha
-
Vlad Zablotskyy
-
Daniel Ha
