Search code examples
djangounit-testingdjango-fixtures

Django testing error installing fixture- no such table


Strange error after running "python manage.py test"

django.db.utils.OperationalError: Problem installing fixture 
'/home/voxa/django/test_test/resume/fixtures/initial_data.json': Could not load 
resume.Person(pk=1): no such table: resume_person

But i've used the same fixtures with "python manage.py loaddata initial_data.json"

UPD:

tests.py

from django.test import TestCase
from django.test import Client

from resume.models import Person

class ResumeTest(TestCase):

    def test_model(self):
        bio = Person(first_name="Homer", last_name="Simpson", birth_date="04.02.1978", email="mail@gmail.com", jabber="jabber@jabbim.com", skype="skype", other_contacts="tel: +380975322155", bio="Was born...")

    def test_index(self):
        client = Client()
        response = client.get('/')
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Volodymyr")

Solution

  • If you're using migrations, your initial_data.json is loaded after every migration. If you want to load a fixture in your tests, do this:

    class ResumeTest(TestCase):
        fixtures = ['my_data']
        ...
    

    See Official django documentation.

    Additionally, rename your 'initial_data.json' to 'my_data.json' to avoid it being loaded after every migration, which is what is probably causing your tests to fail.