濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > django寫(xiě)單元測(cè)試的方法

django寫(xiě)單元測(cè)試的方法

熱門(mén)標(biāo)簽:浙江電銷卡外呼系統(tǒng)好用嗎 地圖標(biāo)注的意義點(diǎn) 地圖制圖標(biāo)注位置改變是移位嗎 南京銷售外呼系統(tǒng)軟件 房產(chǎn)電銷外呼系統(tǒng) 315電話機(jī)器人廣告 上海機(jī)器人外呼系統(tǒng)哪家好 地圖標(biāo)注微信發(fā)送位置不顯示 蓋州市地圖標(biāo)注

   從網(wǎng)上找了很多django單元測(cè)試的案例,感覺(jué)不是很好用,于是自己寫(xiě)了一套測(cè)試方法,在測(cè)試環(huán)境我們只需要傳uri 、請(qǐng)求方式、參數(shù)即可一鍵對(duì)所有接口進(jìn)行測(cè)試。

一、使用requests模擬Http請(qǐng)求

   假設(shè)你執(zhí)行成功的返回的json格式如下:

{
  "code": 0,
  "message": "OK",
  "data": {
    "first": false,
    "token": "3eeeb5bdad75cbe442fd9c6df5373550"
  },
  "elapsed": 96
}

  我寫(xiě)了一個(gè)公共的測(cè)試方法test(),def test(method, url, body_data=None, query_string=None, rest_query_string=None): pass, 傳uri 、請(qǐng)求方式、參數(shù)(query_string,body或者rest都支持)即可,如下代碼可在tests.py文件里執(zhí)行。

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")

我們只需要一鍵執(zhí)行tests.py文件就能看到效果,如下:

二、優(yōu)化代碼將測(cè)試結(jié)果優(yōu)雅地輸出到md文件里

優(yōu)化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返回狀態(tài): " + status + "\n響應(yīng)數(shù)據(jù):\n" + response_data, file=file)


用md編輯器打開(kāi),查看結(jié)果也是非常的直觀:

到此這篇關(guān)于django寫(xiě)單元測(cè)試的方法的文章就介紹到這了,更多相關(guān)django單元測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Django單元測(cè)試中Fixtures的使用方法
  • Django單元測(cè)試中Fixtures用法詳解
  • Django REST framework 單元測(cè)試實(shí)例解析
  • Django單元測(cè)試工具test client使用詳解
  • Django使用unittest模塊進(jìn)行單元測(cè)試過(guò)程解析
  • Python Django框架單元測(cè)試之文件上傳測(cè)試示例
  • 對(duì)Python的Django框架中的項(xiàng)目進(jìn)行單元測(cè)試的方法

標(biāo)簽:貴州 金華 克拉瑪依 日照 臨汾 赤峰 陽(yáng)泉 雙鴨山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《django寫(xiě)單元測(cè)試的方法》,本文關(guān)鍵詞  django,寫(xiě),單元,測(cè)試,的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《django寫(xiě)單元測(cè)試的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于django寫(xiě)單元測(cè)試的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    苏尼特左旗| 马尔康县| 冷水江市| 徐汇区| 当雄县| 武强县| 手游| 营口市| 汉寿县| 桦川县| 镇远县| 新丰县| 台南市| 大竹县| 昆山市| 那坡县| 邯郸县| 安岳县| 东方市| 昌宁县| 南川市| 玉龙| 江门市| 滕州市| 金华市| 临汾市| 社会| 前郭尔| 陇川县| 白银市| 韶山市| 南靖县| 乌拉特后旗| 南皮县| 麟游县| 普定县| 汾西县| 石狮市| 南雄市| 南溪县| 内江市|