简体   繁体   English

测试 Django 时无法加载夹具

[英]Cannot load fixtures when testing Django

I hope not to make a fool of myself by re-asking this question, but I just can't figure out why my fixtures are not loaded when running test.我希望不要通过重新提出这个问题来愚弄自己,但我就是不明白为什么我的装置在运行测试时没有加载。 I am using python 2.7.5 and Django 1.5.3.我正在使用 python 2.7.5 和 Django 1.5.3。

I can load my fixtures with python manage.py testserver test_winning_answers , with a location of survey/fixtures/test_winning_answers.json .我可以使用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

My test class is doing the correct import:我的测试班正在做正确的导入:

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

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

But when trying to run the test command, it cannot find them:但是当尝试运行 test 命令时,它找不到它们:

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)
...

I suspect I am missing something obvious, but cannot quite figure it out... any suggestion would be appreciated.我怀疑我遗漏了一些明显的东西,但无法完全弄清楚......任何建议将不胜感激。 Thanks!谢谢!

It looks like it is trying and failing to find a file called 'initial_data' in '/django/mysite/survey/fixtures' rather than failing to load 'test_winning_answers.json'看起来它正在尝试但未能在“/django/mysite/survey/fixtures”中找到名为“initial_data”的文件,而不是无法加载“test_driving_answers.json”

Try adding a file called 'initial_data.json' containing just '[]'尝试添加一个名为“initial_data.json”的文件,只包含“[]”

I am not sure why, but python manage.py test survey -v3 displays Installed 0 object(s) from 0 fixture(s) .我不知道为什么,但是python manage.py test survey -v3显示Installed 0 object(s) from 0 fixture(s)

Following mariodev's recommendations, I looked into django/test/testcases.py , and changed the call_command('loaddata'...) to use verbosity=3 .按照 mariodev 的建议,我查看了django/test/testcases.py ,并将call_command('loaddata'...)更改为使用verbosity=3 This clearly showed me that the fixture was properly loaded:这清楚地向我表明夹具已正确加载:

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

The error that prompted me to look into fixtures loading was actually a typo I had made...促使我查看装置加载的错误实际上是我犯的一个错字......

在我的情况下,该应用程序从 INSTALLED_APPS 配置中丢失,以便 Django 查找夹具文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM