【发布时间】:2012-09-19 13:10:00
【问题描述】:
我正在使用django_nose 运行我的测试。我有一个夹具initial_data.json,它是一个应用程序的文件夹:my_app/fixtures/initial.json。
夹具在使用python manage.py syncdb 时加载正常,但在运行测试python manage.py test my_app 时根本不加载(虽然我猜它应该加载!?)。任何指向可能出错的指针?
【问题讨论】:
我正在使用django_nose 运行我的测试。我有一个夹具initial_data.json,它是一个应用程序的文件夹:my_app/fixtures/initial.json。
夹具在使用python manage.py syncdb 时加载正常,但在运行测试python manage.py test my_app 时根本不加载(虽然我猜它应该加载!?)。任何指向可能出错的指针?
【问题讨论】:
一旦您创建了一个夹具并将其放置在您的一个 INSTALLED_APPS 的夹具目录中,您就可以通过在您的 django.test.TestCase 子类上指定一个夹具类属性在您的单元测试中使用它:
from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
fixtures = ['mammals.json', 'birds']
def setUp(self):
# Test definitions as before.
call_setup_methods()
def testFluffyAnimals(self):
# A test that uses the fixtures.
call_some_test_code()
【讨论】:
django.utils.unittest.TestCase,这似乎没有导入夹具
我有类似的症状,但有不同的解决方案。我正在通过py.test 而不是manage.py test 运行我的测试。
在appname/fixtures 我有initial_data.json 和foo.json。我的测试看起来像这样:
from django.test import TestCase
class TestFoo(TestCase):
fixtures = ['initial_data.json', 'foo.json']
...
但是,当我运行测试时,initial_data.json 没有被加载,但 foo.json 被加载。
对我来说,解决方案是将initial_data.json 重命名为base.json,然后加载夹具。
我不认为这是原始发布者的问题,但发布此问题以便下一个与我有相同问题的人可以找到一些东西。 :)
【讨论】: