Is it necessary to impliment tearDown to clean up database when using django.test.TestCase?

2,959 views
Skip to first unread message

meppum

unread,
Mar 7, 2008, 1:22:47 PM3/7/08
to Django users
I've been doing something like the following:

MODELS = [SomeObjectType, AnotherObjectType]

class testFoo(TestCase):

def tearDown(self):
for model in MODELS:
model.objecs.all().delete()

Is this how tests should be created, or is all this done behind the
scenes when using TestCase?

-Meppum

Russell Keith-Magee

unread,
Mar 7, 2008, 10:37:35 PM3/7/08
to django...@googlegroups.com

Depends on which TestCase you are using, and the combinations in which
you are using them.

django.test.TestCase does a full database flush at the start of each
new test. This means that you don't need to manually delete objects in
tearDown - the next test setUp will make sure the database is clean.

doctests and unittest.TestCase do no database preparations. At the
start of a test, the database will be in whatever state the previous
test left. This could mean some stray data which could cause testing
conflicts if you are not careful, so a cleanup might be good practice.

Yours,
Russ Magee %-)

Kent Johnson

unread,
Mar 9, 2008, 8:45:10 AM3/9/08
to django...@googlegroups.com

Using unittest.TestCase I do this in setUp(), rather than tearDown, so I
know the test starts with a clean table.

Kent

Reply all
Reply to author
Forward
0 new messages