Django测试没有加载灯具数据 [英] Django test not loading fixture data

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

问题描述

我已经为我正在开发的Django项目编写测试,但是一个特定的灯具无法加载。
夹具是使用dumpdata生成的,我根本就没有装满它。
我可以使用manage.py在该夹具上加载数据,而不会出现错误。我已经验证了使用shell实际加载的数据并查询数据。
这是驱动我坚果,任何帮助将不胜感激。



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

  class ViewsFromUrls(TestCase):
fixtures = [
'centers / fixtures / test_data.json',
'intranet /fixtures/test_data.json',
'training / fixtures / test_data.json',#无法加载
]

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


解决方案

我不知道这是否解决了您的问题,但在本网站上:



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



我发现了一个有趣的话:


您会看到Django搜索 appnames / fixtures和
settings.FIXTURE_DIRS并加载第一个匹配项。所以如果你使用名为
的testdata.json作为你的灯具,你必须确保没有其他的
活动应用程序使用同名的灯具。如果没有,你可以
永远不会确定你实际装载的灯具。因此,
建议您使用应用程序名称,
,例如myapp_testdata.json。


应用此(将appname重命名为文件名中的前缀)解决了我的问题我有同样的问题,如这里所述)


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:

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 .

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天全站免登陆