欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django寫單元測試的方法

 更新時間:2021年04月21日 14:25:17   作者:Dream_it_possible!  
這篇文章主要介紹了django寫單元測試的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

一、使用requests模擬Http請求

   假設你執(zhí)行成功的返回的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 、請求方式、參數(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)化代碼將測試結(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響應數(shù)據(jù):\n" + response_data, file=file)

在這里插入圖片描述
在這里插入圖片描述

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

在這里插入圖片描述

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

相關文章

  • Python實現(xiàn)向QQ群成員自動發(fā)郵件的方法

    Python實現(xiàn)向QQ群成員自動發(fā)郵件的方法

    這篇文章主要介紹了Python實現(xiàn)向QQ群成員自動發(fā)郵件的方法,通過讀取txt文本里的QQ成員數(shù)據(jù)再調(diào)用發(fā)送郵件函數(shù)實現(xiàn)該功能,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • python 如何設置柱狀圖參數(shù)

    python 如何設置柱狀圖參數(shù)

    這篇文章主要介紹了在python中設置柱狀圖參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Centos5.x下升級python到python2.7版本教程

    Centos5.x下升級python到python2.7版本教程

    這篇文章主要介紹了Centos5.x下升級python到python2.7版本教程,本文使用編譯安裝方式,并配置了一系列需要更改的配置項,需要的朋友可以參考下
    2015-02-02
  • python中ctypes使用方法

    python中ctypes使用方法

    這篇文章主要介紹了python中ctypes使用方法,包括 python和c中類型映射,操作結(jié)構(gòu)體和聯(lián)合體,需要定義結(jié)構(gòu)體或聯(lián)合體的類型,然后可以創(chuàng)建實例、訪問其成員等,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個數(shù)的實例

    Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個數(shù)的實例

    今天小編就為大家分享一篇Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • PyQt5事件處理之定時在控件上顯示信息的代碼

    PyQt5事件處理之定時在控件上顯示信息的代碼

    這篇文章主要介紹了PyQt5事件處理之定時在控件上顯示信息的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法

    python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法

    下面小編就為大家分享一篇python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • django中的*args 與 **kwargs使用介紹

    django中的*args 與 **kwargs使用介紹

    這篇文章主要介紹了django中的*args 與 **kwargs使用介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 手把手教你YOLOv5如何進行區(qū)域目標檢測

    手把手教你YOLOv5如何進行區(qū)域目標檢測

    YOLOV5和YOLOV4有很多相同的地方,最大的改變還是基礎架構(gòu)的變化,下面這篇文章主要給大家介紹了關于YOLOv5如何進行區(qū)域目標檢測的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • 不到20行代碼用Python做一個智能聊天機器人

    不到20行代碼用Python做一個智能聊天機器人

    小編先向大家介紹一下本次運用到的python庫,本次項目主要運用到的庫有wxpy和chatterbot。對Python做一個智能聊天機器人的相關知識感興趣的朋友跟隨小編一起看看吧
    2019-04-04

最新評論