【问题标题】:Cannot load fixtures when testing Django测试 Django 时无法加载夹具
【发布时间】:2013-10-24 16:10:45
【问题描述】:

我希望不要通过重新提出这个问题来自欺欺人,但我就是想不通为什么我的固定装置在运行测试时没有加载。我正在使用 python 2.7.5 和 Django 1.5.3。

我可以使用python manage.py testserver test_winning_answers 加载我的固定装置,位置为survey/fixtures/test_winning_answers.json。

Creating test database for alias 'default'...
Installed 13 object(s) from 1 fixture(s)
Validating models...

0 errors found

我的测试类进行了正确的导入:

from django.test import TestCase
class QuestionWinningAnswersTest(TestCase):

    fixtures = ['test_winning_answers.json']
    ...

但是在尝试运行测试命令时,却找不到它们:

python manage.py test survey.QuestionWinningAnswersTest -v3
...
Checking '/django/mysite/survey/fixtures' for fixtures...
...
No json fixture 'initial_data' in '/django/mysite/survey/fixtures'.
...
Installed 0 object(s) from 0 fixture(s)
...

我怀疑我遗漏了一些明显的东西,但无法完全弄清楚......任何建议都将不胜感激。谢谢!

【问题讨论】:

  • 您是否尝试调试TestCase._fixture_setup 以查看加载了哪些灯具?

标签: django testing django-fixtures


【解决方案1】:

看起来它正在尝试并未能在“/django/mysite/survey/fixtures”中找到名为“initial_data”的文件,而不是未能加载“test_winning_answers.json”

尝试添加一个名为“initial_data.json”的文件,其中只包含“[]”

【讨论】:

  • 这确实有效Installing json fixture 'initial_data' from '/django/mysite/survey/fixtures'. CommandError: No fixture data found for 'initial_data'. (File format may be invalid.)。
  • 当然我还是不知道为什么它不接受我要使用的fixture文件。
【解决方案2】:

我不知道为什么,但python manage.py test survey -v3 显示Installed 0 object(s) from 0 fixture(s)。

根据 mariodev 的建议,我查看了 django/test/testcases.py,并将 call_command('loaddata'...) 更改为使用 verbosity=3。这清楚地表明我已正确加载了夹具:

Installing json fixture 'test_winning_answers' from '/django/mysite/survey/fixtures'.
Installed 13 object(s) from 1 fixture(s)

促使我查看固定装置加载的错误实际上是我犯的一个错字...

【讨论】:

    【解决方案3】:

    在我的例子中,应用程序从 INSTALLED_APPS 配置中丢失,以便 Django 找到夹具文件。

    【讨论】:

      猜你喜欢
      • 2013-08-24
      • 2011-03-10
      • 2018-02-07
      • 2011-01-12
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2011-02-13
      相关资源
      最近更新 更多