如何获得有关夹具未装载原因的更多详细信息?

时间:2010-05-25 21:50:44

标签: python django-testing

我有一个似乎没有加载灯具的TestCase。

我正在构建测试数据库时看到此错误:

No fixtures found.  
.............................................Problem installing fixture '/Users/Bryan/work/CNPROG/forum/fixtures/forum_fixtures.json': Traceback (most recent call last):  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle  
    obj.save(using=using)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save  
    models.Model.save_base(self.object, using=using, raw=True)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/base.py", line 543, in save_base  
    created=(not record_exists), raw=raw)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 162, in send  
    response = receiver(signal=self, sender=sender, **named)  
  File "/Users/Bryan/work/CNPROG/forum/models.py", line 656, in record_ask_event  
    activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/fields/related.py", line 302, in __get__  
    rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 341, in get  
    % self.model._meta.object_name)  
DoesNotExist: User matching query does not exist.  



class UserProfileTestCases(TestCase):  
    """These are tests to verify that refactoring of UserProfile is correct"""  
    fixtures = ['forum_fixtures.json'] # apparently this needs to be in fixtures/ directory.  
    def setUp(self):  
        self.client = Client()  
        if self.client.login(username='DickCheney', password='test'):   
            print "client.login DickCheney successful";    
        else:   
            print "client.login FAILED"  

由于某种原因,装置没有加载。

夹具位于:
  forum / fixtures / forum_fixtures.json

如何输出夹具未加载的原因?

Traceback建议在这里发生一些事情:
在record_ask_event中输入文件“/Users/Bryan/work/CNPROG/forum/models.py”,第656行

但我无法想象为什么会影响灯具的加载。 当我查看代码时,通过post_save事件调用record_ask_events 我能够成功manage.py loaddata forum_fixtures所以我相信我正确地设置了它们。

3 个答案:

答案 0 :(得分:1)

使用此命令运行更详细的测试: python manage.py test --verbosity = 2

我通过重新安排灯具的顺序来解决这个问题。

如果您有Related对象,则需要确保在fixture中首先列出具有最多关联的对象(例如auth_user),因为在保存期间可能会调用相关对象。

Django v1.2只查找名为'initial_data'的灯具。没有一次搜索'forum_fixtures.json'

我将灯具名称更改为'initial_data.json',现在可以正常工作。

答案 1 :(得分:0)

您需要更改post_save信号处理程序以尊重'raw'kwarg。如果您使用它做了类似的事情,那么您的信号处理程序在夹具加载期间不会导致错误:

@receiver(post_save, sender=MyModel, dispatch_uid='MyModelUID')
def handler(sender, *args, **kwargs):
    if kwargs.get('raw'):
        return
    else:
        ...stuff

答案 2 :(得分:-1)

我遇到了这个问题。我的应用程序包含在我的设置文件中的INSTALLED_APPS元组中。所以根据django docs,它应该在app目录下的fixtures目录中搜索我放在TestCase类的'fixtures'属性中的任何文件名。不行。不想命名我的夹具'initial_data.json',因为我只想在运行单元测试时使用它。