私はdjangoで書かれた私のプロジェクトのテストケースを書いています.Here {u'message': u'', u'result': {u'username': u'john', u'user_fname': u'', u'user_lname': u'', u'cur_time': 1442808291000.0, u'dofb': None, u'sex': u'M', u'u_email': u'', u'role': u'', u'session_key': u'xxhhxhhhx', u'mobile': None}, u'error': 0}
we can see other field are empty because I just created user in test cases, but not given other info. データベースは本番データベースから作成されますが、初期化されず、空のままです。そのため、他のフィールドを空にしています。空のデータベースを照会しています。
ログイン REST APIの次のテスト ケースを作成しました。python manage.py testで実行します。上記の問題の解き方を教えてください。
注: 次のアプローチが正しくない場合は、他のアプローチを提案できます。
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
import json
class TestAPI(TestCase):
def setUp(self):
self.c=Client() #Create Client object that simulates request to a url similar to a browser can
User.objects.create_user(username="john", password="xxx")
def test_login_api(self):
credential_test=dict()
c_test =Client()
credential_test["username"]="john"
credential_test["password"]="xxx"
data=json.dumps(credential_test)
#print 'data is'
#print data
response_test =c_test.put('/api/login', data)
content_test=json.loads(response_test.content)
print 'content'