On Fri, Jun 26, 2009 at 6:55 AM, Kenneth Gonsalves <law...@thenilgiris.com>wrote:
> > hi, > > this is my first venture into unittests, and have reached a block. Here is > the > contents of my test.py: > > test.py------------------------------------------------------ > > import unittest > from conference.web.models import * > from django.contrib.auth.models import User > > class TalkTestCase(unittest.TestCase): > """ > Create a user, a delegate, a talk category, add a talk > add a tag and test the various methods under Talk model > """ > > def setUp(self): > self.newuser = User.objects.create(username='lawgon', > password='xxxx', > email='law...@localhost.web', > last_name='Gonsalves') > [snip] > > class SlotTestCase(unittest.TestCase): > """Create a conference day, a hall, a user, a delegate, a talk > category, add a talk > give the talk a slot > """ > def setUp(self): > self.newuser = User.objects.create(username='lawgon', > password='xxxx', > email='law...@localhost.web', > last_name='Gonsalves') > [snip] > ----------------------------------------------------end of test.py > > The problem lies in the first line of the class SlotTestCase. The test > crashes > with this error: > IntegrityError: ERROR: duplicate key value violates unique constraint > "auth_user_username_key" > > So I assumed that this record already exists in the database, but when I > use > get() it replies that there is no such user. Any clues as to where I am > going > wrong. > You're deriving your tests from the base Python unittest.TestCase instead of django.test.TestCase: http://docs.djangoproject.com/en/dev/topics/testing/#testcase One of the things that Django's TestCase provides is database isolation -- whatever is done to the DB during one of your unit tests is un-done before the next one is started. You've got two tests there that create the exact same username, the 2nd one to run causes an integrity error unless the DB changes made by the first are cleaned up before the 2nd one runs. Karen --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---