Writing automated tests with Django&Selenium

on Apr 25, 2013 by Ramona

     Getting our Selenium tests to run faster can be complicated at times, because there are a number of factors to be taken into consideration. Debugging these tests is time consuming and the end result is almost always the same one - a large part of the tests needs to be refactored.

      Luckily, for projects implemented with Django, we have the possibility to explore a wide range of testing solutions, backed up by an active internet community.
Instead of always trying to use Selenium as a standalone solution, we could analyze the possibility of having those tests integrated in our Django project, and hence, be able to run with a simple command, all tests. By all tests, I mean unit tests, functional tests (please see related articles on this blog), and Selenium tests (which theoretically should focus more on the JS/CSS/HTML side of the application, rather than on its entire functionality).
     OK, sounds good in theory, but where to from this point?
Although it might seem complicated at first, all you need to do is clone your Django project and start adding your tests in your app’s folder (several apps on be maintained and tested for the same Django project).
      For a practical example, please have a look at this GitHub repo. This is just an example of how Selenium tests can be integrated with Django, with the use of LiveServerTestCase class.
Make sure to run pip install -r requirements.txt and you’re good to go. It is recommended that you use a virtualenv, so that you avoid any conflicts with software already installed on your computer.
     The application used for testing is django-registration, which provides enough support for developing functional tests. We inserted a js library (password_strength_plugin.js) to better illustrate the need of Selenium (JS/CSS/HTML focused) automated tests.
     The advantages of this approach are numerous - you are able to test the build as well, not only the deploy. Tests are faster, and, if you decide to write your tests using a page object pattern method, then debugging will become easier, as you’ll be able to faster track failures and their causes. 

Tags: testing django selenium | Category: testing , python , django Back To Top

comments powered by Disqus