3

运行“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

但我使用了与“python manage.py loaddata initial_data.json”相同的装置

升级版:

测试.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")
4

1 回答 1

5

如果您使用迁移,您的 initial_data.json 会在每次迁移后加载。如果要在测试中加载夹具,请执行以下操作:

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

请参阅官方 django 文档

此外,将您的 'initial_data.json' 重命名为 'my_data.json' 以避免在每次迁移后加载它,这可能是导致测试失败的原因。

于 2014-10-27T10:59:35.293 回答