python3支持在通过requests库调试django后台接口写测试用例

python测试用例库使用

unittest库可以支持单元测试用例编写和验证。

基本使用方法

运行文件可以将文件中的用例全部执行一遍

python 复制代码
import unittest

class TestBasicFunc(unittest.TestCase):
    def test_basic_asert(self):
        self.assertEqual(1, 1)

if __name__=="__main__":
    unittest.main()

运行结果如下图:

基本断言用法

基本断言用法如下:

python 复制代码
import unittest
class TestBasicFunc(unittest.TestCase):
    def test_basic_asert(self):
        self.assertEqual(1, 1)
        self.assertNotEqual(1, 2)
        self.assertAlmostEqual(3.01, 3.02, places=1)
        self.assertNotAlmostEqual(3.1, 2.9, places=2)
        self.assertTrue(3>2)
        self.assertFalse(3<2)
        self.assertIs("Hello", str("Hello"))
        self.assertIn("s", "is a good")

python支持requests库发送网络请求

发送get请求,解析对应返回json文本

python 复制代码
import requests

def try_request_get_test_case():
    url = "http://127.0.0.1:8000/index/"
    headers = {"Content-Type": "application/json"}
    response = requests.get(url, headers=headers)
    return_json = json.loads(response.text)
    print(return_json)
    return return_json

发送post请求

python 复制代码
import requests

def try_request_post_test_case():
    url = "http://127.0.0.1:8000/index/"
    payload = {
        "postWoman" : "localTest"
    }
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, json=payload, headers=headers)
    return_json = json.loads(response.text)
    print(return_json)
    return return_json

django支持解析json格式body

解析json格式的body,需要用.body,而不是get函数

python 复制代码
from django.shortcuts import render
from django.shortcuts import HttpResponse
import json
# Create your views here.
def index(request):
    postWoman=""
    if request.method == "POST":
        print("index!")
        body_json = json.loads(request.body)
        postWoman = body_json["postWoman"]
        print(postWoman)
    data = {
        'name':"hhhh",

        'age':'15',
        'item':"test",
        "postWoman":postWoman
    }
    return HttpResponse(json.dumps(data));

整个测试用例demo

python 复制代码
#!/usr/bin/env python
# coding: utf-8

import requests
import json
import unittest

def try_request_get_test_case():
    url = "http://127.0.0.1:8000/index/"
    headers = {"Content-Type": "application/json"}
    response = requests.get(url, headers=headers)
    return_json = json.loads(response.text)
    print(return_json)
    return return_json

def try_request_post_test_case():
    url = "http://127.0.0.1:8000/index/"
    payload = {
        "postWoman" : "localTest"
    }
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, json=payload, headers=headers)
    return_json = json.loads(response.text)
    print(return_json)
    return return_json

class TestBasicFunc(unittest.TestCase):
    def test_basic_asert(self):
        self.assertEqual(1, 1)
        self.assertNotEqual(1, 2)
        self.assertAlmostEqual(3.01, 3.02, places=1)
        self.assertNotAlmostEqual(3.1, 2.9, places=2)
        self.assertTrue(3>2)
        self.assertFalse(3<2)
        self.assertIs("Hello", str("Hello"))
        self.assertIn("s", "is a good")


class TestRequestGet(unittest.TestCase):
    def test_get_func(self):
        return_json = try_request_get_test_case()
        self.assertEqual(return_json["postWoman"], "")
    def test_post_func(self):
        return_json = try_request_post_test_case()
        self.assertEqual(return_json["postWoman"], "localTest")

if __name__=="__main__":
    unittest.main()
相关推荐
ss2731 分钟前
基于Springboot + vue + 爬虫实现的高考志愿智能推荐系统
spring boot·后端·高考
Bruce-li__16 分钟前
DRF凭什么更高效?Django原生API与DRF框架开发对比解析
数据库·django·sqlite
PXM的算法星球18 分钟前
【软件工程】面向对象编程(OOP)概念详解
java·python·软件工程
专注API从业者43 分钟前
《Go 语言高并发爬虫开发:淘宝商品 API 实时采集与 ETL 数据处理管道》
开发语言·后端·爬虫·golang
Humbunklung1 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(常用类矩阵QRectF)
笔记·python·学习·pyqt
noravinsc1 小时前
connection.cursor() 与 models.objects.filter
数据库·django·原生查询·orm查询
Asthenia04121 小时前
Netty writeAndFlush与Pipeline深入分析
后端
蹦蹦跳跳真可爱5891 小时前
Python----深度学习(基于DNN的吃鸡预测)
python·深度学习·dnn
欧先生^_^1 小时前
Scala语法基础
开发语言·后端·scala
JJ1M82 小时前
Git技巧:Git Hook,自动触发,含实战分享
git·python·自动化