mohi666
mohi666

Reputation: 7072

No fixtures found when testing an app on Django

I have finally decided to create unit test for all my code. That is I'm trying to create fixtures based on my models to test my function against them.

I have create a json fixture using dumpdata command and placed it under fixtures directory on my app. The following is the code on my test:

import unittest
from mysite.myapp.models import Post

class RatingTestCase(unittest.TestCase):
  fixtures = [
      'posts.json',
  ]

def test_me(self):
  p = Post.objects.all()
  self.assertTrue(p)

I run my test using the following command on my Arch Linux machine:

python2.7 manage.py test myapp

It creates a sqlite database and installs all the tables and indeces, however at the end it says no fixtures found and it says my test failed since it didn't find any data.

I'm running the latest revision of development version of Django and noticed that based on the documentation I am supposed to import unittest using:

from django.utils import unittest

However, when I do this, it complains that unittest cannot be imported. That's why I import unittest directly from my python path which worked.

I've tried to mock django model objects before, but I think it's better to test Django apps using fixtures than using mock libraries. Any idea how to load the fixtures?

Thanks in advance.

EDIT: If I change my fixture name to initial_data.json, it will load it every time I run my test. However, I still need to have multiple fixture names for running different tests.

EDIT: I got it working by importing TestCase from the following:

from django.test import TestCase

Upvotes: 5

Views: 5113

Answers (5)

netdigger
netdigger

Reputation: 3789

I had the same problem, and the reason it didnt work for me was that I had no extension on my initial-data-file.

I did:

manage.py dumpdata > initial_data
manage.py loaddata initial_data

Which doesnt work, since there's no extension on initial_data, however, this does work:

mv initial_data initial_data.json 
manage.py loaddata --verbosity=2 initial_data.json 

Upvotes: 0

user2768809
user2768809

Reputation: 1

Just to confirm the import statement for TestCase that breaks finding any fixtures but works in all other ways, so just throws no errors since it attempts no fixture loading is

from django.utils.unittest import TestCase

... DONT USE IT ... as suggested use

from django.test import TestCase

Upvotes: -1

David Dehghan
David Dehghan

Reputation: 24845

I had the name problem. The code below would not work even though I had the file in my fixture directory under the app folder.

fixtures = ['initial_data2.json']

I then changed the import statement to below an it just worked.

from django.test import TestCase

Upvotes: 0

dm03514
dm03514

Reputation: 55972

There are a couple things that could help:

  1. You can run the test with --verbosity=2 to see which directories django looks in for fixtures.
  2. django TestCase is what django recommends to use, opposed to unittest.
  3. Where is your fixture directory/file located? If it's not in your app you have to specify a fixture directory in your settings file.

Upvotes: 5

lprsd
lprsd

Reputation: 87225

Have you checked if the path where the fixture is present is available in:

settings.FIXTURE_DIRS = ('/path/to/proj_folder/fixtures/',)

Upvotes: 0

Related Questions