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.

http://docs.djangoproject.com/en/dev/topics/testing/

http://www.python.org/doc/2.5.2/lib/organizing-tests.html

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.

image

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.

image

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

image

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

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: