Django is ignoring my TestCase fixtures

951 Views Asked by At

I've got Django 1.4. In my test.py, I've got the requisite TestCase import:

from django.test import TestCase

To isolate the issue, I've added the line:

fixtures = ['westeros']

to the default example test case, i.e.

class SimpleTest(TestCase):
    fixtures = ['westeros']

    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)

Using django-admin.py dumpdata, I created a fixture file called "westeros" in my customers/fixtures directory, where "customers" is an app that is listed in settings.INSTALLED_APPS.

When I run the test, at any verbosity, Django simply ignores the fixture and passes the test_basic_addition test. No error, no fixture loading. It's as if the TestCase import isn't there. Any ideas on what could be wrong or how to debug this?

2

There are 2 best solutions below

1
On BEST ANSWER

It's ok to omit the extension when defining fixtures as you have done, i.e.

fixtures = ['westeros']

However, the fixture file itself must have the extension that corresponds to its serializer e.g westeros.json, westeros.json.zip or westeros.xml for json, zipped json or xml respectively.

1
On

Where is your westeros file located?

It needs to either be in a fixtures directory inside an app or in the dir specified by FIXTURE_DIRS in your settings.py file

You can run with tests with verbosity=2 to get full output. https://docs.djangoproject.com/en/1.0/ref/django-admin/#test

Is your fixtures file named westeros ? or does it have a file extension?

Django will fail silently on fixture loads as you see. (at least up until 1.3, I haven't used fixtures in new 1.4 version yet). But you are not actually testing if the fixtures are loading. Throw in a self.assertGreater(YourModel.objects.all(), 0) or somethign to verify that there are no objects, or drop in a debbuger and start querying some of your models.