2010-09-24 30 views

回答

4
#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'] 
+4

是沒可能加載測試用例沒有樣板django.test.TestCase子? – epoch 2010-09-24 08:40:34

+1

1)創建shell腳本:首先加載裝置:django-admin.py loaddata foo/bar/mydata.json;運行測試邏輯;結束; 2)你可以從python載入燈具:從django.core導入管理management.call_command('loaddata','fixture1.json',詳細= 0) – iddqd 2010-09-24 08:43:09

2

在你的設置方法,只要致電:

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

然後在你的拆卸,請致電:

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

你可以從這裏進口管理:

from django.core import management 
+0

接受的答案不適用於我。這是。 – galarant 2012-10-19 22:29:03

+0

http://pythontesting.net/framework/nose/nose-fixture-reference/你可以在包級別使用這些命令 – Simanas 2014-06-13 12:18:41

2

只要將測試用例作爲FastFixtureT的一個子類即可estCase。

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