gpt4 book ai didi

django - 为什么 Django-1.3 找不到我的单元测试装置?

转载 作者:行者123 更新时间:2023-12-01 15:05:10 25 4
gpt4 key购买 nike

我正在尝试在单元测试中使用我的装置。

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。

最佳答案

django.test导入测试用例;

  • 不是: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

关于django - 为什么 Django-1.3 找不到我的单元测试装置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5886534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com