Testing Django Applications

Tessie the Test Machine

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.

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

  • The other piece of the puzzle here is to have a network of Selenium servers. And a Python module that does this. Then, you can say:

    def setUp(self):
        self.start_test_server('0.0.0.0', 8000)
        self.selenium = SeleniumCluster(browser="*pifirefox", os="windows xp", port=8000)
  • Yep. That's certainly the next step.
  • This is great! This is a great way to keep the development database clean while using a test database for the Selenium tests. I've been running into this recently too as I build up my test suite. It's always hard to not "mess up" the database and cause Selenium headaches on the tests.

    I'm definitely downloading this and using it. Thanks!
  • Yeah. When I sat down to start writing Selenium tests, I figured that would happen. Even if you're careful, it's just a matter of time before something goes wrong and you're in there manually fixing the database.

    So when I saw this ticket on Django's trac, I figured that might actually be a better way to spend my time than being extremely careful and praying.

    Turned out not that hard. A few python threading issues asside. I certainly feel better knowing that our Selenium tests won't go haywire and require a bunch of time to fix.
  • The database is clearly defined. Great.
  • Pretty awesome! Maybe I missed it but how are you creating the charts at the end? Google Charts API?
  • Yes. We're using the Google Charts API for graphing some stuff to visualize internally. It's our pulse of what's happening when it's happening.
  • Russell Keith-Magee
    Thanks for the contribution, Devin! Let me assure you that adding live testserver support is on my radar as something to add to Django's native testing services.

    I've only had a chance to take a cursory glance at the patch you submitted, but on the surface, it looks good.
    I won't get a chance to merge it into trunk before v1.0, but with any luck I will get it into the next release cycle.
  • Oh no rush. Certainly not important with the 1.0 push going on.
  • Graham
    keep up the good work
  • I wonder If I can use disqus with my iphone device?
  • Hi Daniel,
    Is there a way to post to Disqus using a blackberry?
  • You can make replies to posts through email, and since you can reply to emails on a blackberry, the answer is yes. Just reply to the notification that was sent to you.
  • Regis
    Excellent news!
  • Testing.. disqus...
  • enricodesimone
    Interventismo e mercato regolato: e' contraddizione tra emergenza e libero mercato
  • Thats good to hear ! Thanks for the hardwork ! Keep it up
  • Hi Guys,

    I just noticed something interesting on my blog. Disqus happened to be the first plugin I installed, and the only one so far. The FF status bar is showing "www.google-analitycs.com"..... while I have no such code installed on my blog.

    I would love some sort of explanation.
  • Disqus uses GA for analytics. Many websites and widgets do. It allows
    us to better measure usage without having to analyze our own server
    logs. There is no personal data involved.

    Do you have any other questions about it? Email me.
  • Daniel,

    I am aware many use GA. In fact I use it on my sites and blogs.... But
    I keep it on my blog and on my site. I find it strange that your GA
    measures things on my blog.
  • No. We're measuring Disqus hits. It has little to do with your site -
    only that the hits are coming from your site.

    We can measure the same without GA, like any 3rd party application/
    widget. GA just makes it easier.
  • Daniel,

    I would love to e-mail you. However I am not privileged to your e-mail
    address. I just wrote a post why I am leaving Disqus at
    http://sageblogger.com/ - if you care that is.
  • It's in my profile - daniel@disqus.com
blog comments powered by Disqus