django-nose includes a fixture bundler which drastically speeds up your tests
by eliminating redundant setup of Django test fixtures. To use it…

Subclass django_nose.FastFixtureTestCase instead of
django.test.TestCase. (I like to import it asTestCase in my
project’s tests/__init__.py and then import it from there into my actual
tests. Then it’s easy to sub the base class in and out.) This alone will
cause fixtures to load once per class rather than once per test.

Activate fixture bundling by passing the --with-fixture-bundling option
to ./manage.pytest. This loads each unique set of fixtures only once,
even across class, module, and app boundaries.

The fixture bundler reorders your test classes so that ones with identical sets
of fixtures run adjacently. It then advises the first of each series to load
the fixtures once for all of them (and the remaining ones not to bother). It
also advises the last to tear them down. Depending on the size and repetition
of your fixtures, you can expect a 25% to 50% speed increase.

Incidentally, the author prefers to avoid Django fixtures, as they encourage
irrelevant coupling between tests and make tests harder to comprehend and
modify. For future tests, it is better to use the “model maker” pattern,
creating DB objects programmatically. This way, tests avoid setup they don’t
need, and there is a clearer tie between a test and the exact state it
requires. The fixture bundler is intended to make existing tests, which have
already committed to fixtures, more tolerable.

If using --with-fixture-bundling causes test failures, it likely indicates
an order dependency between some of your tests. Here are the most frequent
sources of state leakage we have encountered:

Locale activation, which is maintained in a threadlocal variable. Be sure to
reset your locale selection between tests.

memcached contents. Be sure to flush between tests. Many test superclasses do
this automatically.

It’s also possible that you have post_save signal handlers which create
additional database rows while loading the fixtures. FastFixtureTestCase
isn’t yet smart enough to notice this and clean up after it, so you’ll have to
go back to plain old TestCase for now.

In some unusual cases, it is desirable to exempt a test class from fixture
bundling, forcing it to set up and tear down its fixtures at the class
boundaries. For example, we might have a TestCase subclass which sets up
some state outside the DB in setUpClass and tears it down in
tearDownClass, and it might not be possible to adapt those routines to heed
the advice of the fixture bundler. In such a case, simply set the
exempt_from_fixture_bundling attribute of the test class to True.

The default Django TransactionTestCase class can leave the DB in an unclean
state when it’s done. To compensate, TransactionTestCase does a
time-consuming flush of the DB before each test to ensure it begins with a
clean slate. Django’s stock test runner then runs TransactionTestCases last so
they don’t wreck the environment for better-behaved tests. django-nose
replicates this behavior.

Some people, however, have made subclasses of TransactionTestCase that clean up
after themselves (and can do so efficiently, since they know what they’ve
changed). Like TestCase, these may assume they start with a clean DB. However,
any TransactionTestCases that run before them and leave a mess could cause them
to fail spuriously.

django-nose offers to fix this. If you include a special attribute on your
well-behaved TransactionTestCase…

classMyNiceTestCase(TransactionTestCase):cleans_up_after_itself=True

…django-nose will run it before any of those nasty, trash-spewing test cases.
You can thus enjoy a big speed boost any time you make a TransactionTestCase
clean up after itself: skipping a whole DB flush before every test. With a
large schema, this can save minutes of IO.

django-nose’s own FastFixtureTestCase uses this feature, even though it
ultimately acts more like a TestCase than a TransactionTestCase.

If you have a model that is used only by tests (for example, to test an
abstract model base class), you can put it in any file that’s imported in the
course of loading tests. For example, if the tests that need it are in
test_models.py, you can put the model in there, too. django-nose will make
sure its DB table gets created.