在Django中加载Selenium测试夹具时的完整性错误

时间:2014-12-16 06:47:29

标签: django selenium fixtures django-testing

我想为我的硒测试加载一个夹具。在我的初始测试中使用灯具是成功的,所以我知道我能够在我的测试设置中加载灯具并在我的测试中使用它们。我尝试了几种方法。 首先,我生成了特定于我使用dumpdata测试的模型的灯具。一个例子如下:

python manage.py dumpdata protocols.Step --indent=2 > functional_tests/fixtures/step.json

在我的测试中使用时如此:

class SignInTest(FunctionalTest):
    fixtures = ['admin_user.json', 'protocol.json', 'step.json',
             'protocol_element.json']

    def test_login_and_view_user_data(self):
        ...

我收到错误:

django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'protocols_protocolelement' with primary key '37' has an invalid foreign key: protocols_protocolelement.element_content_type_id contains a value '41' that does not have a corresponding value in django_content_type.id.

第二次尝试涉及在我的表中使用所有测试数据,但不包括contenttypes:

python manage.py dumpdata --indent=2 -e contenttypes > functional_tests/fixtures/initial_data.json

class SignInTest(FunctionalTest):
    fixtures = ['initial_data.json']
    ...

获取错误:

django.db.utils.OperationalError: Problem installing fixture '.../mike/mike/functional_tests/fixtures/initial_data.json': Could not load auth.Permission(pk=103): no such table: auth_permission

接下来,我尝试使用自然来显示自然键:

python manage.py dumpdata --natural -e contenttypes -e auth.Permission --indent=2 > functional_tests/fixtures/initial_data2.json

只是为了得到错误:

django.db.utils.OperationalError: Problem installing fixture '.../mike/mike/functional_tests/fixtures/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user

注意到自然是折旧的我试过--natural-foreign并且想要包含用户和权限模型(无论如何我需要内容类型):

python manage.py dumpdata --natural-foreign --indent=2 > functional_tests/fixtures/initial_data3.json

只是为了得到错误:

django.db.utils.IntegrityError: Problem installing fixture '.../mike/mike/functional_tests/fixtures/initial_data3.json': Could not load contenttypes.ContentType(pk=35): UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

那么,关于如何加载夹具以便我可以运行测试的任何想法?我有什么简单的遗失吗?谢谢!

1 个答案:

答案 0 :(得分:11)

在更多关于Django如何维护自己的模型等的阅读之后,我的理解是Django缓存了contenttype,auth.Permission等,并在测试框架中使用它们(我使用的是StaticLiveServerTestCase)。这意味着当我加载我的夹具时,它与Django为自己的用途存储的数据冲突,导致完整性错误。这对我有用:

python manage.py dumpdata -e contenttypes -e admin -e auth.Permission --natural-foreign --indent=2 > functional_tests/fixtures/initial_data4.json

这篇文章有一些额外的有用信息可以帮助我解决问题: Problems with contenttypes when loading a fixture in Django

相关问题