【问题标题】:initial_data.json not loading before testsinitial_data.json 在测试前未加载
【发布时间】: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 testing nose


    【解决方案1】:

    我引用official documentation

    一旦您创建了一个夹具并将其放置在您的一个 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 版本 initial_data.json 以前是自动加载的...另外,注意不要使用django.utils.unittest.TestCase,这似乎没有导入夹具
    • 谢谢,测试套件不会自动使用夹具对我来说绝对不直观。测试套件不也做一个同步数据库,所以你希望它加载 initial_data?
    【解决方案2】:

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

    appname/fixtures 我有initial_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,然后加载夹具。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-03
      • 2010-11-12
      • 1970-01-01
      • 2015-11-06
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多