django写单元测试的方法

编程入门 行业动态 更新时间:2024-10-26 21:26:02

   从网上找了很多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-8from django.test import testcase, clientimport osimport requestsimport jsonuser = "1234567"host = "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文件就能看到效果,如下:

二、优化代码将测试结果优雅地输出到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编辑器打开,查看结果也是非常的直观:

到此这篇关于django写单元测试的方法的文章就介绍到这了,更多相关django单元测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

django写单元测试的方法

本文发布于:2023-06-10 22:56:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   方法   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!