jeudi 1 février 2018

Django test - models.DoesNotExist: matching query does not exist

I have two test files, test1.py and test2.py. Individually they run fine, but when I try to run them using 'django manage.py test' I get the following error:

post.models.DoesNotExist: Post matching query does not exist.

One more thing I notice is that, whichever file runs first will work well while the other throws this error (tested this by renaming files). I am using django.test.TestCase. This is the relevant code -

test1.py:

def setUpTestData(cls):
    Post.objects.create(...)
    Post.objects.get(id=1) #Works fine since this file runs first

test2.py:

def setUpTestData(cls):
    Post.objects.create(...)
    Post.objects.get(id=1) #Throws above error

I am new to testing in django so I think I'm missing something basic here.

Aucun commentaire:

Enregistrer un commentaire