为什么Django-1.3没有为UnitTest找到我的灯具?

时间:2011-05-04 16:15:45

标签: django unit-testing fixtures

我正在尝试在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.  

看起来固定装置没有加载。

我已经能够使用灯具来填充数据库,但由于某种原因,在测试中找不到灯具。

路径是正确的,但我无法弄清楚出了什么问题。

$ ls /Users/Bryan/work/osqa/fixtures/fixture_questions.json 
/Users/Bryan/work/osqa/fixtures/fixture_questions.json

以更高的详细程度运行测试表明没有找到灯具。 我正在运行Django 1.3。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

您没有将完整路径传递给灯具,只是灯具名称:

fixtures = ['fixture_questions.json']  

只要夹具位于INSTALLED_APPS中的应用程序中的fixtures目录中,Django就会找到它。