2

I'm trying to load some fixtures within my Django tests, but they don't seem to load.

In my settings.py, I specify:

FIXTURE_DIRS = (os.path.join(PROJECT_DIR, 'dhtmlScheduler\\fixtures\\'))

Now, within my test case:

def setUp(self):
    fixtures = ['users.json', 'employee.json']

I should also probably mention that I'm using the Nose test runner:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

and unittest:

class TestEmployee(unittest.TestCase):

I must be missing something obvious, can someone point me in the right direction?

james_dean
  • 1,477
  • 6
  • 26
  • 37
  • 1
    Unrelated to the question: Use a raw string for your directory there: `r'dhtmlScheduler\fixtures\'`. Easier to read. :) – Luke Sneeringer Mar 19 '13 at 16:27
  • Note to Luke and others: "Note that these paths should use Unix-style forward slashes, even on Windows." [source](https://docs.djangoproject.com/en/4.2/ref/settings/#fixture-dirs) – Jarad Aug 02 '23 at 18:22

3 Answers3

7

FIXTURE_DIRS is supposed to be a list or tuple, not a string. Remember that it's the comma that defines a tuple litteral, not the parens, IOW your settings should be

FIXTURE_DIRS = (
    os.path.join(PROJECT_DIR, 'dhtmlScheduler\\fixtures\\'), 
    )

As a side note, hardcoding the path separator kind of defeats the whole point of using os.path.join(), so this should really be:

FIXTURE_DIRS = (
    os.path.join(PROJECT_DIR, 'dhtmlScheduler', 'fixtures'), 
    )

Edit : and finally, you have to declare your TestCase fixtures at the class level, not in the setUp() method...

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1
FIXTURE_DIRS = (os.path.join(PROJECT_ROOT, 'fixtures'),)

Or

from django.test import TestCase

class MyTestCase(TestCase):
     fixtures = [
        '/myapp/fixtures/users.json', 
        '/myapp/fixtures/employee.json'
     ]
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
catherine
  • 22,492
  • 12
  • 61
  • 85
0

In your test case file, Simply make reference to your predefined fixtures in this manner as shown below enter image description here

Adépòjù Olúwáségun
  • 3,297
  • 1
  • 18
  • 27