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