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

use*_*703 7 django selenium fixtures django-testing

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

python manage.py dumpdata protocols.Step --indent=2 > functional_tests/fixtures/step.json
Run Code Online (Sandbox Code Playgroud)

在我的测试中用作如下:

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

    def test_login_and_view_user_data(self):
        ...
Run Code Online (Sandbox Code Playgroud)

我收到错误:

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.
Run Code Online (Sandbox Code Playgroud)

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

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

class SignInTest(FunctionalTest):
    fixtures = ['initial_data.json']
    ...
Run Code Online (Sandbox Code Playgroud)

得到错误:

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
Run Code Online (Sandbox Code Playgroud)

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

python manage.py dumpdata --natural -e contenttypes -e auth.Permission --indent=2 > functional_tests/fixtures/initial_data2.json
Run Code Online (Sandbox Code Playgroud)

只是为了得到错误:

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
Run Code Online (Sandbox Code Playgroud)

注意自然是折旧我试过 - 自然 - 外国,并希望包括用户和权限模型(我需要内容类型为我的模型):

python manage.py dumpdata --natural-foreign --indent=2 > functional_tests/fixtures/initial_data3.json
Run Code Online (Sandbox Code Playgroud)

只是为了得到错误:

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
Run Code Online (Sandbox Code Playgroud)

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

use*_*703 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
Run Code Online (Sandbox Code Playgroud)

这篇文章有一些额外的有用信息可以帮我解决问题:在Django中加载一个fixture时出现contenttypes的问题 .