Django REST 与 pytest 夹具完整性错误
我正在尝试使用 pytest 创建一个固定装置,但是当我运行它时,出现以下错误:
django.db.utils.IntegrityError: NOT NULLconstraint failed: authtoken_token.user_id
我找到了有关它的信息,但我不知道当涉及到固定装置时如何处理。
我的代码:
@pytest.fixture
def client(db):
api_client = APIClient()
token = Token.objects.get_or_create(user__username='testuser')
api_client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
return api_client
@pytest.mark.django_db
def test_get_list_test(client):
url = reverse('api/lists')
response = client.get(url)
assert response.status_code == 200
I am trying to create a fixture with pytest, but when I run it, I get the following error:
django.db.utils.IntegrityError: NOT NULL constraint failed: authtoken_token.user_id
I found information on it, but I am not sure how to handle it when it comes to fixtures.
My code:
@pytest.fixture
def client(db):
api_client = APIClient()
token = Token.objects.get_or_create(user__username='testuser')
api_client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
return api_client
@pytest.mark.django_db
def test_get_list_test(client):
url = reverse('api/lists')
response = client.get(url)
assert response.status_code == 200
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,我不认为你的错误与这里的 pytest 有关。
我认为这里的主要问题与 django 数据库迁移有关 看这里
所以首先,无论任何框架(pytest),请确保修复它,
并且在成功验证错误消失后(也手动检查),尝试运行夹具。
Generally speaking, i don't think your error related to pytest here.
I think the main issue here related to django db migrations look here
So first, Make sure you fix it, regardless to any framework (pytest)
And after you have successfully validated that the error passed away (check manually also), try to run the fixture.