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.

Blog at WordPress.com.