Django real world functional testing
20 Jan 2012
In the real world, a django application may be deployed on a real production plateform with as many layers as needed:
When we write functional tests with the dummy web client, we do not cover what’s going on over all those layers that composed application globality.
Also, in the real world, we use web client intances such as browsers like chromium, opera, IE… to deal with the application over HTTP and we want our application to run correctly on all those clients.
That’s why we need to write functionnal tests that cover real world constraints.
Django 1.4 will bring us tools to do the job.
Lets write a demo application
To simply illustrate this, I’ve made a simple (useless) application composed of a single user story:
As an anonymous user, I can write a message, then the message is displayed in a list with the nine latest messages.
Ok, now, I’ll write a functional test case basically using the dummy client:
and then modify views.py to feet my need:
Django 1.4 introduced a new test class called LiveServerTestCase that extends TransactionTestCase.
This new test class launches the application in a real WSGI server instance into an asynchronous thread. As it’s a real server, we can easily write tests within real web browser instances.
There are many solutions providing python backends (Ghost.py, Selenium…), here I’m gonna use Selenium.
So, I had a new (minimalist) test case:
As you can see, we can deal with database and browser in the same script…
That’s all for now.