django 单元测试实操
import json import random import uuid import demjson from django.test import TestCase from rest_framework.test import APIClient from back_server.models import CertificationSource class LittleTestCase(TestCase): def setUp(self): self.client = APIClient() self.test_cs = CertificationSource.objects.create(**{"id":uuid.uuid1(node=random.randint(0,281474976710655)) ,"cs_name": "ni_ldap_test00", "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com", "base_dn": "OU=ww,DC=aa,DC=ss", "user_filter": "dsdsdfa", "remark": "test", "user_cn": "CN=users,OU=xx,DC=xx,DC=com", "state": False}) self.test_cs1 = CertificationSource.objects.create(**{"id":uuid.uuid1(node=random.randint(0,281474976710655)), "cs_name": "ni_ldap_test08", "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com", "base_dn": "OU=ww,DC=aa,DC=ss", "user_filter": "dsdsdfa", "remark": "test", "user_cn": "CN=users,OU=xx,DC=xx,DC=com", "state": False}) self.test_cs2 = CertificationSource.objects.create(**{"id": uuid.uuid1(node=random.randint(0, 281474976710655)), "cs_name": "ni_ldap_test09", "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com", "base_dn": "OU=ww,DC=aa,DC=ss", "user_filter": "dsdsdfa", "remark": "test", "user_cn": "CN=users,OU=xx,DC=xx,DC=com", "state": False}) def test_users_post(self): form_data = {"id":uuid.uuid1(node=random.randint(0,281474976710655)), "cs_name": "ni_ldap_test011", "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com", "base_dn": "OU=ww,DC=aa,DC=ss", "user_filter": "dsdsdfa", "remark": "test", "user_cn": "CN=users,OU=xx,DC=xx,DC=com", "state": False } r = self.client.post(path="/iam/api/v1/cs", data=form_data,) result = r.json() self.assertEqual(result['msg'], '创建成功') def test_get_cs(self): r = self.client.get(path="/iam/api/v1/cs",content_type='application/json') result = r.json() self.assertEqual(result['result']['data'][0]['cs_name'], 'ni_ldap_test00') def test_put_cs(self): form_data = {"cs_name": "ni_ldap_test02", "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com", "base_dn": "OU=ww,DC=aa,DC=ss", "user_filter": "dsdsdfa", "remark": "test", "user_cn": "CN=users,OU=xx,DC=xx,DC=com", "state": False } r = self.client.put(path="/iam/api/v1/cs?cs_id="+str(self.test_cs.id),data=form_data) result = r.json() self.assertEqual(result['msg'], '更新成功') self.assertEqual(result['result']['data']['cs_name'], 'ni_ldap_test02') def test_get_cs_by_id(self): r = self.client.get(path="/iam/api/v1/cs?cs_id="+str(self.test_cs.id),content_type='application/json') result = r.json() self.assertEqual(result['result']['data'][0]['cs_name'], 'ni_ldap_test00') def test_delete_cs_by_id(self): r = self.client.delete(path="/iam/api/v1/cs?cs_id=" + str(self.test_cs.id), content_type='application/json') result = r.json() self.assertEqual(result['msg'], '删除认证源成功') def test_batch_enable_cs(self): cs_id_list= [str(self.test_cs2.id),str(self.test_cs1.id)] last_cs_id_list=demjson.encode(cs_id_list) r = self.client.post(path="/iam/api/v1/batch/certification?option=" +"enable",data=last_cs_id_list,content_type='application/json' ) result = r.json() self.assertEqual(result['msg'], '启用成功') def test_batch_disable_cs(self): cs_id_list= [str(self.test_cs2.id),str(self.test_cs1.id)] last_cs_id_list=demjson.encode(cs_id_list) # r = self.client.post(path="/iam/api/v1/batch/certification?option=" +"disable",data=cs_id_list,content_type='application/json' ) r = self.client.post(path="/iam/api/v1/batch/certification?option=" +"disable",data=last_cs_id_list,content_type='application/json') result = r.json() self.assertEqual(result['msg'], '禁用成功') def test_batch_delete_cs(self): cs_id_list= [str(self.test_cs2.id),str(self.test_cs1.id)] last_cs_id_list=demjson.encode(cs_id_list) r = self.client.post(path="/iam/api/v1/batch/certification?option=" + "delete", data=last_cs_id_list,content_type='application/json') result = r.json() self.assertEqual(result['result']['data']['delete_success_list'], cs_id_list)
path参数:就是除去域名和端口号之后的路径 如 127.0.0.1:8000/iam/api/v1/batch/certification?options=enable
此时path就写 path=/iam/api/v1/batch/certification?options=enable
APIClient:
self.client = APIClient()
要这样用
而不是用requests 用requests需要起动django 项目 并且 操作是在原数据库下操作的
输入命令:python manage.py test
就可以启动测试脚本了
浙公网安备 33010602011771号