我对django的单元测试有中等的知识。有人可以帮我进行单元测试吗?
我有这样的API:
elif request.method == "POST":
note = json.loads(request.body)['note']
note_objs = Notes.objects.filter(client_user=client_user, expert_user=expert_user)
if not note_objs:
note_obj = Notes()
else:
note_obj = note_objs[0]
note_obj.client_user = client_user
note_obj.expert_user = expert_user
note_obj.notes = note
note_obj.save()
return self.create_response(request, {
'success': True,
'note': note_obj.notes,
})
单元测试如下:
# POST note
data = {
"note": "testnotes",
}
resp = self.api_client.post(self._get_note(client_user), data=data, format='json',
authentication=self.get_credentials(expert_user))
self.assertHttpOK(resp)
json_resp = self.deserialize(resp)
self.assertEquals(json_resp.get('note'), 'testnotes')
self.assertEquals(json_resp['success'], True)
运行正常。我想知道如何使用API片段中的if和else进行单元测试。
此致