Tuesday, February 28, 2012

Re: Django Unittesting Question.

On 28/02/2012, at 7:10 PM, Johan wrote:

> Hi. I can see that the setUp method of my TestCase specialization is
> being run for each test case. I thought that the test DB is cleared
> every time before setUp is called. Is this the case? I'm having a
> problem where I am assuring that only one of a certain record will be
> in the DB but because the db isn't cleared I end up with two records
> and this breaks my code.

It depends whether you're using Django's TestCase (django.tests.TestCase) or unittest's TestCase (unittest.TestCase).

The vanilla unittest.TestCase does nothing to manage your database. If you access a database during the TestCase, it's also your responsibility to clean up afterwards.

django.test.TestCase is a subclass of unittest.TestCase, so anything that works in a normal Python TestCase will also work in a Django TestCase. The Django TestCase subclass just adds a bunch of extra tools to make testing database-backed websites easier.

One of the most important of these extra tools is database clearing. If you use django.test.TestCase, the database will be cleared before the start of every test. If you create database objects during the test (during setUp, or during the test itself), that data will be cleared out before the start of the next test.

Russ Magee %-)

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment