I'm trying to use my fixtures in a UnitTest.
AddFavoritesTestCase(unittest.TestCase):
fixtures = ['/Users/Bryan/work/osqa/fixtures/fixture_questions.json']
def setUp(self):
self.factory = RequestFactory()
def testAdminCanFavorite(self):
user = User.objects.get(pk=3)
...
self.assertEqual(response.status_code, 200)
======================================================================
ERROR: testAdminCanFavorite (forum.tests.tests_building_stickyness.AddFavoritesTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Bryan/work/osqa/forum/tests/tests_building_stickyness.py", line 18, in testAdminCanFavorite
user = User.objects.get(pk=3) # Kallie has admin
File "/usr/local/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/db/models/manager.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "/Users/Bryan/work/osqa/forum/models/base.py", line 64, in get
obj = super(CachedQuerySet, self).get(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/db/models/query.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: User matching query does not exist.
It seems the fixtures are not loading.
I've been able to use the fixtures to populate the database, but for some reason the fixtures aren't being found in the tests.
The path is correct but I can't figure out what's going wrong.
$ ls /Users/Bryan/work/osqa/fixtures/fixture_questions.json
/Users/Bryan/work/osqa/fixtures/fixture_questions.json
Running the test at a higher verbosi开发者_StackOverflow中文版ty shows that the fixtures are not being found. I'm running Django 1.3.
Import the TestCase from django.test
;
- Not:
import unittest
- Not:
import django.utils.unittest
- But:
import django.test
Like this:
from django.test import TestCase
class test_something(TestCase):
fixtures = ['one.json', 'two.json']
...
https://docs.djangoproject.com/en/1.3/topics/testing/#django.test.TestCase
You don't pass the full path to the fixture, just the fixture name:
fixtures = ['fixture_questions.json']
As long as the fixture is in a fixtures
directory within an app that's in INSTALLED_APPS, Django will find it.
精彩评论