django写单元测试的方法
作者:Dream_it_possible! 时间:2021-02-04 19:17:28
从网上找了很多django单元测试的案例,感觉不是很好用,于是自己写了一套测试方法,在测试环境我们只需要传uri 、请求方式、参数即可一键对所有接口进行测试。
一、使用requests模拟Http请求
假设你执行成功的返回的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-8
from django.test import TestCase, Client
import os
import requests
import json
user = "1234567"
host = "http://localhost:8006/app"
false = False
true = True
null = None
token = None
POST = "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"] = token
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")
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文件就能看到效果,如下:
二、优化代码将测试结果优雅地输出到md文件里
优化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编辑器打开,查看结果也是非常的直观:
来源:https://blog.csdn.net/qq_33036061/article/details/115914998
标签:django,单元测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Django中的unittest及应用
2022-10-24 08:26:54
SQL Server元数据的管理与应用
2009-03-16 14:24:00
Mysql中explain的使用详解
2009-12-08 16:18:00
微软建议的ASP性能优化28条守则(3)
2008-02-24 16:30:00
有时用户并不需要引导
2009-07-17 18:48:00
![](https://img.aspxhome.com/file/UploadPic/20097/17/ceshi-96s.png)
费明红:什么样的404页面才是正确的?
2009-05-21 17:47:00
![](https://img.aspxhome.com/file/UploadPic/20095/21/1-50s.jpg)
Javascript"篱式"条件判断(翻译)
2008-08-01 12:21:00
python GUI模拟实现计算器
2023-01-08 22:20:40
![](https://img.aspxhome.com/file/2023/6/66906_0s.jpg)
python可视化 matplotlib画图使用colorbar工具自定义颜色
2023-01-06 07:10:01
![](https://img.aspxhome.com/file/2023/2/94022_0s.png)
简单介绍Python中的JSON模块
2023-12-30 00:16:29
![](https://img.aspxhome.com/file/2023/7/99007_0s.png)
Python 学习教程之networkx
2023-10-11 01:49:07
客户端JavaScript代码封装
2008-12-26 18:10:00
Django项目在pycharm新建的步骤方法
2021-01-19 11:46:32
Centos7下编译安装配置Nginx+PHP+MySql环境
2023-11-14 19:10:06
oracle 时间格式的调整
2009-05-24 19:32:00
django manage.py扩展自定义命令方法
2023-11-03 20:55:42
![](https://img.aspxhome.com/file/2023/5/64225_0s.jpg)
ASP-server.URLEncode反函数:urldecode
2008-10-23 16:05:00
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
2022-07-31 05:54:21
![](https://img.aspxhome.com/file/2023/3/76033_0s.gif)
php中重定向网页跳转方法总结案例教程
2023-06-11 20:47:18
Python 函数list&read&seek详解
2022-02-14 03:04:21
![](https://img.aspxhome.com/file/2023/7/86447_0s.png)