【问题标题】:Django load test fixtures with django-noseDjango 负载测试夹具与 django-nose
【发布时间】:2011-04-16 16:03:10
【问题描述】:

如何使用 django-nose 测试运行程序加载测试夹具?

【问题讨论】:

    标签: django unit-testing tdd nosetests


    【解决方案1】:
    #settings.test.py 
    INSTALLED_APPS += ('django_nose', )
    TEST_RUNNER = 'django_nose.run_tests'
    
    #appname/tests.py
    from datetime import date,datetime, timedelta
    from django.contrib.auth.models import User
    from django.test.client import Client
    from django.test import TestCase
    
    class BetViewsTestCase(TestCase):
        #files placed in appname/fixtures/restaurant.json, appname/fixtures/map.json
        fixtures = ['authtestdata.json', 'restaurant.json', 'map.json']
    

    【讨论】:

    • 没有样板 django.test.TestCase 子类就不能加载测试用例吗?
    • 1) 创建shell脚本:首先加载fixtures:django-admin.py loaddata foo/bar/mydata.json;运行测试逻辑;结尾; 2)你可以从python加载fixture: from django.core import management management.call_command('loaddata', 'fixture1.json', verbosity=0)
    【解决方案2】:

    在您的设置方法中,只需调用:

    management.call_command('loaddata', 'Category.json', verbosity=0)
    

    然后在你的拆解中,调用:

    management.call_command('flush', verbosity=0, interactive=False)
    

    您可以从这里导入管理:

    from django.core import management
    

    【讨论】:

    【解决方案3】:

    只需将测试用例设为 FastFixtureTestCase 的子类即可。

    from django_nose import FastFixtureTestCase
    from myapp.models import MyModel
    from nose_tools import eq_
    
    class TestFixtureLoading(FastFixtureTestCase):
        fixtures = ['mymodel_data.yaml']
    
        def test_fixture_loading(self):
            eq_(1, MyModel.objects.count())
    

    然后:

    python manage.py test
    

    【讨论】:

      猜你喜欢
      • 2012-11-11
      • 2018-02-07
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 2011-06-07
      • 2013-10-24
      • 2015-06-28
      相关资源
      最近更新 更多