Automating Django Testing for GIS Web Applications using LiveReload

Automating Django Testing for GIS Web Applications using LiveReload

Automating Django Testing for GIS Web Applications using LiveReloadWhen building any web application, geospatial or otherwise, testing is important. Writing and running unit tests is the first line of defense against regression. The Django framework makes writing and running tests very simple, which is part of what makes it such a powerful framework.

One problem with testing, however, is that to reap it’s benefits fully you must be diligent about regularly running your tests. If your project has not already implemented a continuous integration solution such as Jenkins, this probably means going to a shell and running a command to execute your test scripts. Testing is meant to maximize developer productivity, but the need to manually run tests pulls developers out of their flow which has a negative effect on productivity. This is part of why continuous integration is such an important tool.

However, for some projects the overhead of setting up a continuous integration solution may be prohibitive (especially smaller, experimental projects). Towards this end, for a hackathon project I worked on, I came up with a solution for automating the running of Django test scripts – using software that was already running on my machine, specifically LiveReload and Growl.

Examples of automated testing for GIS / Geospatial web applications

An automated testing solution could be used in many interesting ways. For example:

  • Testing user access to various GIS data resources based on user roles for a CalTrans project planning web application
  • Testing GIS map layer services in the Arches cultural heritage mapping application
  • Testing Django’s access to geospatial data stored in a Neo4J graph database as part of a project evaluating Neo4J’s suitability for storing Arches cultural heritage GIS data

The LiveReload solution

I’ve been using LiveReload for a couple years now and it has become an invaluable productivity tool for me. It is incredibly fast to set up, and saves me thousands of keystrokes when I’m working on front-end code. LiveReload polls your project code for changes and automatically refreshes a browser so you can seamlessly move between your code and a testing environment.

My thought was that I could leverage the fact that LiveReload is already polling for file changes to detect when my application code has changed and execute my Django test scripts in the background without any intervention from me (and without diverting my attention from the code more than necessary). It turned out that this was incredibly simple to do, and the results were surprisingly potent.

To do this, I had to make a couple simple changes to my LiveReload configurations. First I had to configure it to poll for changes to python files (*.py) as by default LiveReload only polls for changes to front-end code. Next I had to configure LiveReload to execute my test scripts when changes are observed. Since the output from these commands is not sent anywhere by default, I had to pipe the output (including stderr) to growlnotify as described in the LiveReload knowledge-base ( http://feedback.livereload.com/knowledge
base/articles/86219-custom-post-processing
).

The complete shell command that I configured is as follows:

       python manage.py test 2>&1 | growlnotify

Automating Django Testing for GIS Web Applications using LiveReload in Python

Automating Django Testing for GIS Web Applications using LiveReload

Now, whenever I save a change to my application code, my test scripts are executed in the background and the output is displayed as a self-dismissing Growl notification in the corner of my display!

Automating Django Testing for GIS Web Applications using LiveReload Growl Notification

This means no more manually running tests and I’ll never be left unaware when a change that I’ve made has broken a test. That’s pretty powerful for such a simple solution!

 

Related articles

FARL_Divider_Graphic-cropped
Deploying ArcGIS Portal and Your First Web Applications with the LGIM
Use your mobile phone to find the best fishing hotspots near you
Port of San Francisco Uses Enterprise GIS to Prepare for Sea Level Rise Caused by Climate Change