我正在尝试在UnitTest中使用我的fixtures。
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. 看起来固定装置不能加载。
我已经能够使用fixture来填充数据库,但是由于某些原因,在测试中找不到fixture。
路径是正确的,但我不知道出了什么问题。
$ ls /Users/Bryan/work/osqa/fixtures/fixture_questions.json
/Users/Bryan/work/osqa/fixtures/fixture_questions.json以更高的详细级别运行测试会显示未找到fixture。我运行的是Django 1.3。
发布于 2011-06-02 15:10:27
从django.test导入TestCase;
import unittest
import django.utils.unittest
import django.test不是
如下所示:
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
发布于 2011-05-05 00:26:15
您不需要将完整路径传递给fixture,只需传递fixture名称:
fixtures = ['fixture_questions.json'] 只要该装置在INSTALLED_APPS应用程序的fixtures目录中,Django就会找到它。
https://stackoverflow.com/questions/5886534
复制相似问题