我正在使用django_nose
来运行我的测试。我有一个夹具initial_data.json
这是一个应用程序的文件夹:my_app/fixtures/initial.json
。initial_data.json在测试之前未加载
当使用python manage.py syncdb
时灯具加载正常,但在运行测试python manage.py test my_app
时(虽然我猜它应该加载!),但根本不加载。任何可能出错的指针?
我正在使用django_nose
来运行我的测试。我有一个夹具initial_data.json
这是一个应用程序的文件夹:my_app/fixtures/initial.json
。initial_data.json在测试之前未加载
当使用python manage.py syncdb
时灯具加载正常,但在运行测试python manage.py test my_app
时(虽然我猜它应该加载!),但根本不加载。任何可能出错的指针?
一旦你创建了一个夹具并把它放在一个fixtures目录在你的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()
我有类似的症状,但不同的解决方案。我通过py.test
而不是manage.py test
运行我的测试。我有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
,然后装入夹具。
我不认为这是原来的海报的问题,但发布这个,以便与我有同样问题的下一个人可以找到一些东西。 :)
它的作品!非常感谢 !我很确定,在以前的Django版本中<= 1.3? 'initial_data.json'用于自动加载...此外,要小心不要使用'django.utils.unittest.TestCase',这似乎不会导入灯具 – sebpiq
谢谢,这绝对不是我的直觉,测试套件不会自动使用灯具。测试套件是否也会执行syncdb,因此您希望它加载initial_data? – Nils