在Django中加载硒测试夹具时出现完整性错误

在Django中加载硒测试夹具时出现完整性错误,django,selenium,fixtures,django-testing,Django,Selenium,Fixtures,Django Testing,我想为我的selenium测试加载一个夹具。在我的初始测试中使用fixture是成功的,所以我知道我能够在测试设置中加载fixture并在测试中使用它们。我尝试了几种方法。 首先,我使用dumpdata生成了特定于我正在测试的模型的装置。一个例子如下: python manage.py dumpdata protocols.Step --indent=2 > functional_tests/fixtures/step.json 在我的测试中使用时,因此: class SignInTes

我想为我的selenium测试加载一个夹具。在我的初始测试中使用fixture是成功的,所以我知道我能够在测试设置中加载fixture并在测试中使用它们。我尝试了几种方法。 首先,我使用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.
第二次尝试涉及使用我的表中的所有测试数据,但不包括ContentType:

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
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
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
接下来,我尝试使用natural显示自然关键点:

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.Permission(pk=103): no such table: auth_permission
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
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
注意到natural被贬低了,我尝试了--natural foreign,并希望包括用户和权限模型(无论如何,我的模型需要contenttypes):

仅获取错误:

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
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
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

那么,关于如何加载夹具以便运行测试,有什么想法吗?有什么简单的东西我遗漏了吗?谢谢

在详细阅读了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
这篇文章还有一些帮助我解决问题的有用信息: