#Django #TransactionTestCase with REUSE_DB=1 of #django-nose

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 presetup 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.

[sourcecode language=”python”]
from django.test import TransactionTestCase
from django.db import connections, DEFAULTDBALIAS

def flushdb(cls):
    if getattr(cls, ‘multidb’, False):
        databases = connections
    else:
        databases = [DEFAULT
DBALIAS]
    for db in databases:
        management.call
command(‘flush’, verbosity=0,
            interactive=False, database=db)

class BaseTransactionTestCase(TransactionTestCase):
    @classmethod
    def tearDownClass(cls):
        flushdb(cls)

[/sourcecode]

Check the next post: Time flies by »

Share on:
comments powered by Disqus