【发布时间】:2011-04-16 16:03:10
【问题描述】:
如何使用 django-nose 测试运行程序加载测试夹具?
【问题讨论】:
标签: django unit-testing tdd nosetests
如何使用 django-nose 测试运行程序加载测试夹具?
【问题讨论】:
标签: django unit-testing tdd nosetests
#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']
【讨论】:
在您的设置方法中,只需调用:
management.call_command('loaddata', 'Category.json', verbosity=0)
然后在你的拆解中,调用:
management.call_command('flush', verbosity=0, interactive=False)
您可以从这里导入管理:
from django.core import management
【讨论】:
只需将测试用例设为 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
【讨论】: