我有以下测试
Class FriendsTest(TestCase):
# always login user
def setUp(self):
self.user = User.objects.all().order_by('date_joined')[0]
self.client.login(facebook_id = self.user.get_profile().facebook_id)
def test_test(self):
r = self.client.get(reverse('web.views.friends.main'))
self.assertEqual(r.status_code, 200)
def test_test2(self):
r = self.client.get(reverse('web.views.friends.main'))
self.assertTemplateUsed(r, 'web/friends.html')
# and some test for GET data here.
如果我只有一种测试方法(test_test),则测试有效。但是当我有多个方法(test_test2)时,我不断收到错误“IntegrityError:无法加载web.CustomProfile(pk = 1):(1062,“密钥'id_user'的重复条目'1'”)“。
错误跟踪如下。我真的很感激任何帮助。
.问题安装夹具'/Users/ruixia/public_html/x/project/web/fixtures/data.json':追溯(最近一次通话):
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/core/management/commands/loaddata.py”,第 196 行,在句柄 obj.save(using=using)
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/core/serializers/base.py”,第165行,保存models.Model.save_base(self.object, using=using ,原始=真)
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/base.py”,第 551 行,在 save_base 结果 = manager._insert([self], fields=fields , return_id=update_pk, using=using, raw=raw)
_insert return insert_query(self.model, objs, fields, **夸格斯)
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/query.py”,第 1576 行,在 insert_query return query.get_compiler(using=using).execute_sql(return_id )
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/sql/compiler.py”,第910行,在execute_sql cursor.execute(sql, params)
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/backends/mysql/base.py”,第 114 行,在执行中返回 self.cursor.execute(query, args)
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/MySQLdb/cursors.py”,第201行,在执行self.errorhandler(self, exc, value)
文件“/Users/ruixia/virt_env/x/lib/python2.7/site-packages/MySQLdb/connections.py”,第 36 行,在 defaulterrorhandler 中引发 errorclass,errorvalue IntegrityError: Could not load web.CustomProfile(pk=1) :(1062,“重复条目'1'键'id_user'”)