I am writing a test cases for my project written in django, it's giving an unexpected output that looks like {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}
Here we can see other field are empty because I just created user in test cases, but not given other info. database is created from the production database, but not initialized, it remains empty. That's why it is giving other field empty. It is querying empty database.
I have written following test case for login REST API. and running it by python manage.py test. Please tell me how to solve above problem.
Note: If following approach is not correct then you can suggest other approach.
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'