Reputation: 7072
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
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
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
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
Reputation: 55972
There are a couple things that could help:
Upvotes: 5
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