Django supplies it’s own TestCase class which wraps each test in a transaction. This means any changes a test makes to the test database(s) are rolled back when the test has finished. It also means that test fixtures are reloaded for each test.

I wanted to test a substantial data set, and still have succinct tests. Using the Django TestCase was proving too slow, so I repurposed some of the code from TestCase into two functions:

from django.db import connections
from django.db import transaction


def disable_transaction_methods():
    from django.test.testcases import disable_transaction_methods

    for db in connections:
        transaction.enter_transaction_management(using=db)
        transaction.managed(True, using=db)
    disable_transaction_methods()


def restore_transaction_methods():
    from django.test.testcases import restore_transaction_methods

    restore_transaction_methods()
    for db in connections:
        transaction.rollback(using=db)
        transaction.leave_transaction_management(using=db)</pre>

These can then be used by the class setup and teardown methods that [nose][3] supports, so the fixtures load once per TestCase:

# See https://gist.github.com/3830479 for disable_ and restore_ functions

from unittest import TestCase

class Test(TestCase):

    @classmethod
    def class_setup(cls):
        disable_transaction_methods()
        # ... import test fixtures

    @classmethod
    def class_teardown(cls):
        restore_transaction_methods()

    def test_data_attributes(self):
        # ...

    def test_class_properties(self):
        # ...

[3]: http://nose.readthedocs.org/