本篇文章給大家分享的是有關使用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-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文件就能看到效果,如下:

優化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怎么編寫一個單元測試功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。