我正在尝试为我的 django 项目运行以下单元测试:
from django.test import TestCase
from django.contrib.auth.models import User
from CarbonEmissions import models
class DbTest(TestCase):
#is called before each test case (e.g test_insertingUserProfiles)
def setUp(self):
self.user = User.objects.create(username='ppoliani')
self.userProfile = models.UserProfile.objects.create(user=self.user, title='Mr', type='student', occupation='student')
def test_insertingUserProfiles(self):
"""
Testing the insertion of user profiles into our datbase
"""
self.assertEqual(self.user.get_profile().title,'Mr')
#is called after each test case (e.g test_insertingUserProfiles)
def tearDown(self):
self.user.delete()
self.userProfile.delete()
测试失败,抛出以下错误:
IntegrityError: duplicate key value violates unique constraint "CarbonEmissions_userprofile_user_id_key" DETAIL: Key (user_id)=(1) already exists.
我无法理解该代码有什么问题。