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()
相关推荐
破无差8 分钟前
python实现简单的地图绘制与标记20250705
python
用户3966144687199 分钟前
TypeScript 系统入门到项目实战-慕课网
后端
guojl15 分钟前
Dubbo SPI原理与设计精要
后端
Lemon程序馆16 分钟前
搞懂 GO 的垃圾回收机制
后端·go
用户81221993672226 分钟前
React18+Next.js14+Nest.js全栈开发复杂低代码项目-仿问卷星
后端
喜欢吃豆30 分钟前
目前最火的agent方向-A2A快速实战构建(二): AutoGen模型集成指南:从OpenAI到本地部署的全场景LLM解决方案
后端·python·深度学习·flask·大模型
寻月隐君35 分钟前
Rust 网络编程实战:用 Tokio 手写一个迷你 TCP 反向代理 (minginx)
后端·rust·github
汪子熙44 分钟前
理解 git checkout 与 git reset 的联系和区别
后端
程序员爱钓鱼1 小时前
Go 语言泛型 — 泛型语法与示例
后端·面试·go
好开心啊没烦恼1 小时前
Python 数据分析:DataFrame,生成,用字典创建 DataFrame ,键值对数量不一样怎么办?
开发语言·python·数据挖掘·数据分析