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()
相关推荐
吃面不喝汤661 小时前
Flask + Swagger 完整指南:从安装到配置和注释
后端·python·flask
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
AI原吾5 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
毕设木哥5 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
weixin_455446175 小时前
Python学习的主要知识框架
开发语言·python·学习
D11_6 小时前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.6 小时前
python基础知识(四)--if语句,for\while循环
python
IT毕设梦工厂7 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
luthane8 小时前
python 实现average mean平均数算法
开发语言·python·算法