Django 测试未加载夹具数据 [英] Django test not loading fixture data

查看:25
本文介绍了Django 测试未加载夹具数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我正在处理的 Django 项目编写了测试,但是一个特定的装置无法加载.该夹具是使用 dumpdata 生成的,我根本没有摆弄它.我可以在该夹具上使用 manage.py 加载数据而不会出错.我已经验证了数据实际上是使用 shell 加载并查询数据.这让我发疯,任何帮助将不胜感激.

I have written tests for a Django project that i am working on, but one particular fixture fails to load. The fixture is generated using dumpdata and i havent fiddled with it at all. I can load the data using manage.py on that fixture without errors. I have verified that the data actually loaded using shell and querying the data. This is driving me nuts, any help would be much appreciated.

这是我的测试文件(删除了不相关的部分):

Here is my test file (irrelevant portions removed):

class ViewsFromUrls(TestCase):
    fixtures = [
        'centers/fixtures/test_data.json',
        'intranet/fixtures/test_data.json',
        'training/fixtures/test_data.json', #The one that fails to load
        ]

    def setUp(self):
        self.c = Client()
        self.c.login(username='USER', password='PASS')

    ...

    def test_ViewBatch(self):
        b = Batch.objects.all()[0].ticket_number
        response = self.c.get(reverse('training.views.view_batch', kwargs={'id':b}))
        self.assertTrue(response.status_code, 200)
    ...

推荐答案

我不确定这是否能解决您的问题,但在此站点上:

I Am not sure if this fixes your problem, but on this site:

https://code.djangoproject.com/wiki/Fixtures

我发现了一个有趣的评论:

I found an interesting remark:

你看到 Django 搜索 appnames/fixtures 和settings.FIXTURE_DIRS 并加载第一个匹配项.所以如果你使用名字像 testdata.json 一样用于你的装置,你必须确保没有其他活动应用程序使用同名的装置.如果没有,你可以永远不要确定您实际加载了哪些灯具.因此它是建议您在灯具前加上应用程序名称,例如myapp/fixtures/myapp_testdata.json .

you see that Django searches for appnames/fixtures and settings.FIXTURE_DIRS and loads the first match. So if you use names like testdata.json for your fixtures you must make sure that no other active application uses a fixture with the same name. If not, you can never be sure what fixtures you actually load. Therefore it is suggested that you prefix your fixtures with the application names, e.g. myapp/fixtures/myapp_testdata.json .

应用这个(在文件名中使用 appname 作为前缀重命名装置),解决了我的问题(我遇到了与这里描述的相同的问题)

Applying this (renaming the fixtures with appname as prefix in the filename), solved my problem (I had the same issue as described here)

这篇关于Django 测试未加载夹具数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆