Utility Pattern

November 15, 2008

Unit Testing in Django

Filed under: Django, Python — utilitypattern @ 12:33 pm

I love unit testing. I need a framework for organizing the unit tests for my Django budget app. Fortunately, the fine folks at Django have put significant thought into this already.



I just need to narrow down my options.

Use Python’s unittest module

Doctests look interesting, but I’ve previously written reams of unit tests using Python’s unittest module for other projects. So, we’ll go with that.

We’ll put the tests in a file called ‘tests.py’ in the application directory. The manage.py utility will look here for unit tests and run them automatically if it is invoked with the test option.

Running the Django unit tests in Eclipse with Pydev

You can run your unit tests within Eclipse (with Pydev) if you set the run configuration properly. This is nicer than the command line because the errors are hyperlinked to the lines in the source modules.

Open the run configuration for the manage.py module of your Django project.


My project is called budget and because I’m just getting started, my application is called basic. Now I can tweak the run arguments for manage.py to run the unit tests in the basic application.


You also need to tell Django where its settings are. Just set the DJANGO_SETTINGS_MODULE environment variable.


Now, you can run the unit tests from within Eclipse.

September 30, 2008

Django and Eclipse

Filed under: Django, Python — utilitypattern @ 5:08 am

I’m just getting started with Django and I’m using Eclipse. I’m assuming you already know about Pydev, which is excellent. If you don’t, and you want to develop in Python, go check it out. Go ahead, I’ll wait.

Ok. Here’s a couple of little things that make life a even better.

First, take a look at the excellent Django tutorial.


Then, check out some notes from Fabio Zadrozny, the developer of Pydev on how to configure Pydev to work with Django.


Development Server

We want a quick and dirty debug server that we can start and stop quickly in Eclipse. So, we need to edit the run configuration of manage.py in the Django project and add the following.

runserver –-noreload


Now you can start the Django development server with a quick Ctrl+F11. As Fabio points out, you can’t stop the server with Ctrl-Break, but the red stop button in the Consol works just fine.


With Reload

I also like to run the server with the reload at a command prompt. Make sure you have python in your path. I switched to this about midway through the tutorial. I got tired of restarting the server.

Python Console

Another thing that is nice, especially as you work through the tutorials, is a proper interactive Python console. Adding the following in the Initial interpreter commands tells Django which project you are working on, without having to define an environment variable in the OS. Just change ‘testproj’ to the name of your project.

import os
os.environ[‘DJANGO_SETTINGS_MODULE’]= ‘testproj.settings’


Blog at WordPress.com.