Lately, I found out that Django’s TransactionTestCase leaves test data in database after the test case is executed. It’s not until the next execution of _pre_setup method of a TransactionTestCase instance that the database is flushed. This is troublesome when tests are run with Django Nose’s test runner with REUSE_DB =1.
An easy fix to this is to customize the TransactionTestCase so that it deletes the test data on exit. I wrote a simple wrapper around Django’s TransactionTestCase and extend it to write other transaction test cases.
from django.test import TransactionTestCase from django.db import connections, DEFAULT_DB_ALIAS def flushdb(cls): if getattr(cls, 'multi_db', False): databases = connections else: databases = [DEFAULT_DB_ALIAS] for db in databases: management.call_command('flush', verbosity=0, interactive=False, database=db) class BaseTransactionTestCase(TransactionTestCase): @classmethod def tearDownClass(cls): flushdb(cls)