Unit test tools

Test runners like nose and ​py.test make writing and running unittests easier, and they also make setting up your django enviroment for testing fairly painless. With either package, you can write module-level fixtures (setup/teardown) to install your app models before you run your tests and get rid of them after you're done. Here's a simple example, using sqlite in memory for the test database:

If you want to use a persistent db for testing, you'll probably want to follow the lead of runtests.py in django/tests and drop your test database when you're done with it.

Doctest tools

When writing doctests, creating model classes in the doctest is a natural thing to want to do, but it can be hard to get just the right incantation to create the test models' database tables, especially if you have other apps in your test settings. This function can make things quite a bit easier, as its own doctest demonstrates:

defcreate(module_name,*models):"""Create tables for a group of models in the current db. This
makes it a bit easier to create django models in doctests.
You have to have set up your django.settings, etc, properly
beforehand, of course. But assuming that is done, you can use
create to create test models in fake modules like so:
>>> class Baz(models.Model):
... name = models.CharField(maxlength=10)
... def __repr__(self):
... return self.name
... class Meta:
... app_label = 'baz'
>>> create('foo.bar.baz.models', Baz)
>>> b = Baz(name='a baz')
>>> b.save()
>>> Baz.objects.all()
[a baz]
Take special care with the app_label setting. It must be present,
and it must match the 2nd to last part of the fake module name
passed to create.
"""# create a fake module to hold the modelsimportimp
testmod = imp.new_module(module_name)for model in models:setattr(testmod, model.__name__, model)# create them in the db fromdjango.dbimport connection
fromdjango.coreimport management
sql = management.get_sql_create(testmod)
cursor = connection.cursor()for statement in sql:
cursor.execute(statement)