initial_data.json在测试之前没有加载

时间:2012-09-19 13:10:00

标签: django testing nose

我正在使用django_nose来运行我的测试。我有一个夹具initial_data.json,它是一个应用程序的文件夹:my_app/fixtures/initial.json

使用python manage.py syncdb时夹具加载正常,但在运行测试python manage.py test my_app时根本不加载(我猜它应该加载!?)。任何可能错误的指针?

2 个答案:

答案 0 :(得分:3)

我引用official documentation

  

一旦你创建了一个fixture并把它放在你的一个INSTALLED_APPS的fixtures目录中,你可以在你的单元测试中通过在你的django.test.TestCase子类上指定一个fixtures类属性来使用它:

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()

答案 1 :(得分:2)

我有类似的症状,但有不同的解决方案。我通过py.test而不是manage.py test运行我的测试。

appname/fixturesinitial_data.jsonfoo.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,然后加载灯具。

我不认为这是原始海报的问题,但发布这个以便下一个与我有同样问题的人可以找到一些东西。 :)