Cra*_*pus 3 django unit-testing django-fixtures
运行“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
Run Code Online (Sandbox Code Playgroud)
但是我使用了与“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")
Run Code Online (Sandbox Code Playgroud)
如果您使用迁移,则您的 initial_data.json 会在每次迁移后加载。如果要在测试中加载夹具,请执行以下操作:
class ResumeTest(TestCase):
fixtures = ['my_data']
...
Run Code Online (Sandbox Code Playgroud)
请参阅官方 django 文档。
此外,将您的 'initial_data.json' 重命名为 'my_data.json' 以避免在每次迁移后加载它,这可能会导致您的测试失败。
| 归档时间: |
|
| 查看次数: |
3206 次 |
| 最近记录: |