Django test not loading fixture data

百般思念 提交于 2019-12-03 15:23:22

问题


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

回答1:


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)




回答2:


Import the TestCase from django.test:

from django.test import TestCase

class test_something(TestCase):
    fixtures = ['one.json', 'two.json']
    ...
  • Not: import unittest
  • Not: import django.utils.unittest
  • But: import django.test

That's a day of frustration right there. Stop complaining - it's in the docs :-/




回答3:


Check if the fixture is really in the right place. From the docs:

Django will search in three locations for fixtures:

  1. In the fixtures directory of every installed application
  2. In any directory named in the FIXTURE_DIRS setting
  3. In the literal path named by the fixture



回答4:


One thing to note, when creating the FIXTURE_DIRS constant in your settings file, be sure to leave out the leading '/' if you have a general fixtures directory off of the root of your project.

Ex:
'/actual/path/to/my/app/fixtures/'

Now, in the settings.py file:
Will NOT work:
FIXTURE_DIRS = '/fixtures/'

Will work:
FIXTURE_DIRS = 'fixtures/'

It's possible this depends on how your other routes are configured, but it was a gotcha that had me scratching my head for a little while. Hope this is useful. Cheers.



来源:https://stackoverflow.com/questions/4655038/django-test-not-loading-fixture-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!