【问题标题】:Unable to load fixture data in Django tests无法在 Django 测试中加载夹具数据
【发布时间】:2011-03-10 15:25:08
【问题描述】:

我有一个名为 pta.apps.users 的应用程序,该应用程序已加载到我的 settings.py 的已加载应用程序设置中。

该应用程序运行良好,我现在想对其进行测试,并将 json 文件放在我的用户应用程序内的 fixtures 目录中。

我有一个运行良好的测试,但固定装置无法加载。

from django.test import TestCase
from django.test.client import Client
from BeautifulSoup import BeautifulSoup

class SimpleTest(TestCase):

  fixtures = ['user.json']

  def setUp(self):
    self.c = Client()

  def test_parse_html(self):
    response = self.c.get('/users/login/', follow=True)
    soup = BeautifulSoup(response.content)
    self.assertEquals(soup.h1.renderContents(), 'Entrance')

我的输出中只有No fixtures found.。我正在使用 Django 1.2.1。任何帮助将不胜感激。

【问题讨论】:

    标签: python django testing django-admin


    【解决方案1】:

    要加载一个夹具,我很确定您必须使用 loaddata 手动调用它(就像您所做的那样),或者专门调用它“initial_data”,例如initial_data.sqlinitial_data.jsoninitial_data.yaml等,并将其存储在您应用的fixtures目录中。

    https://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

    【讨论】:

      【解决方案2】:

      你把你的固定装置放在哪里了? 固定装置应放在您的应用程序目录中:

      MyApp/fixtures/user.json
      

      或者你必须在你的settings.py中指定一个外部的fixtures目录:

      FIXTURE_DIRS = (
          'external_fixtures/',
      )
      

      【讨论】:

      • 是的,这就是我拥有它们的地方 MyApp/fixtures/user.json 干杯理查德
      • 奇怪的是我可以使用 python manage.py loaddata user.json 直接从 loaddata 加载数据
      猜你喜欢
      • 2013-10-24
      • 2011-06-07
      • 2013-08-24
      • 2018-02-07
      • 2011-01-12
      • 1970-01-01
      • 2022-11-03
      • 2014-12-20
      • 1970-01-01
      相关资源
      最近更新 更多