本篇文章给大家分享的是有关使用Django怎么编写一个单元测试功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、使用requests模拟Http请求 假设你执行成功的返
本篇文章给大家分享的是有关使用Django怎么编写一个单元测试功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
假设你执行成功的返回的JSON格式如下:
{ "code": 0, "message": "OK", "data": { "first": false, "token": "3eeeb5bdad75cbe442fd9c6df5373550" }, "elapsed": 96}
我写了一个公共的测试方法test(),def test(method, url, body_data=None, query_string=None, rest_query_string=None): pass
, 传uri 、请求方式、参数(query_string,body或者rest都支持)即可,如下代码可在tests.py文件里执行。
from djanGo.test import TestCase# Create your tests here.# coding:utf-8from django.test import TestCase, Clientimport osimport requestsimport jsonuser = "1234567"host = "http://localhost:8006/app"false = Falsetrue = Truenull = Nonetoken = NonePOST = "POST"GET = "GET"DELETE = "DELETE"PUT = "PUT"headers = {'content-Type': 'application/json', 'Accept': '*/*'}login_data = json.dumps({"phone": user, "pwd": "e10adc3949ba59abbe56e057f20f883e", "login_type": 0, "identifier": "", "role": 0})login = requests.post(host + "/login", data=login_data, headers=headers)login_content = eval(login.content.decode("utf-8"))if login_content["code"] == 0: print("login 成功") token = login_content["data"]["token"] print("token:" + token)else: print("login fail")if not token: raise Exception("登录异常")headers["user-token"] = tokendef test(method, url, body_data=None, query_string=None, rest_query_string=None): if query_string: url = host + url + (str(rest_query_string) if rest_query_string is not None else "") + "?" + query_string else: url = host + url + (str(rest_query_string) if rest_query_string is not None else "") if method in [POST, DELETE, PUT] and body_data: body_data = json.dumps(body_data) response_data = requests.request(method, url, data=body_data, headers=headers) response_data = response_data.content.decode("utf-8") if response_data.find("\"code\": 0") != -1: print(url + " 成功!") else: print(url + " 失败!" + response_data)test(GET, "/check_token/", rest_query_string=token)test(GET, "/get/child")
我们只需要一键执行tests.py文件就能看到效果,如下:
优化test方法, 添加样式,md文件支持读取样式。
def test(method, url, body_data=None, query_string=None, rest_query_string=None): if query_string: url = host + url + (str(rest_query_string) if rest_query_string is not None else "") + "?" + query_string else: url = host + url + (str(rest_query_string) if rest_query_string is not None else "") if method in [POST, DELETE, PUT] and body_data: body_data = json.dumps(body_data) response_data = requests.request(method, url, data=body_data, headers=headers) response_data = response_data.content.decode("utf-8") status = "<font color='red'>失败</font>" if response_data.find("\"code\": 0") != -1: status = "<font color='green'>成功</font>" print(url + " 成功!") else: print(url + " 失败!") response_data = "```json\n" + response_data + "\n```" print("url: " + url + "\n返回状态: " + status + "\n响应数据:\n" + response_data, file=file)
用md编辑器打开,查看结果也是非常的直观:
以上就是使用django怎么编写一个单元测试功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。
--结束END--
本文标题: 使用django怎么编写一个单元测试功能
本文链接: https://www.lsjlt.com/news/274184.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
2024-05-21
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0