Django 2021年最新版教程35python+request+unittest 对Django接口进行测试

Posted 软件工程小施同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 2021年最新版教程35python+request+unittest 对Django接口进行测试相关的知识,希望对你有一定的参考价值。

对于接口类函数,即被GET、POST调用的函数

更多情况请参考下面的文章和代码示例

stduypython+request+unittest 接口测试练习

https://www.cnblogs.com/gaoyuanyuan/p/9837771.html

import requests
import unittest
"""嘉宾查询接口"""
class GuestQuery(unittest.TestCase):
 
    def setUp(self):
        self.url="http://127.0.0.1: 8000/api/get_event_list/"
    """使用错误的请求方式post 进行测试"""
    def test_method_post(self):
        res = requests.post(self.url)
        data = res.json()
        self.assertEqual(data["status"],"100" )
        self.assertEqual(data["message"],"请求方法错误")
 
    """使用get正确查询"""
    def test_method_get(self):
        res = requests.get(self.url)
        data = res.json()
        # print(data)
        self.assertEqual(res.status_code,200)
        self.assertEqual(data["status"],"110)")
        self.assertEqual(data["message"],"查询成功")
 
"""嘉宾添加接口"""
class GuestAdd(unittest.TestCase):
    def setUp(self):
        self.url = "http://127.0.0.1: 8000/api/add_event/"
        self.headers = {'Content-Type': 'application/json'}
    """使用错误的请求方法get 进行测试"""
    def test_get_guest(self):
        res = requests.get(self.url)
        data = res.json()
        self.assertEqual(res.status_code,200)
        self.assertEqual(data["status"],"404" )
        self.assertEqual(data["message"],"请求方法错误")
 
    """必填参数为空"""
    def test_add_guest_params_null(self):
        json ={}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        print(data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10020" )
        self.assertEqual(data["message"],"parameter null")
    """必填参数 值为空"""
    def test_add_guest_params_value_null(self):
        json = {
                "event_id": "",
                "realname": "",
                "phone": "",
                "email": "",
                "sign": ""}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        print(data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10021" )
        self.assertEqual(data["message"],"parameter error")
    """发布会id为不存在"""
    def test_add_guest_event_id(self):
        json = {
            "event_id": 88888,
            "realname": "张三",
            "phone": "15210001000",
            "email": "152100@qq.com",
            "sign": "0"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10022" )
        self.assertEqual(data["message"],"event id null")
    """发布会id为非整型"""
    def test_add_guest_event_id(self):
        json = {
            "event_id": "123",
            "realname": "张三",
            "phone": "15210001000",
            "email": "152100@qq.com",
            "sign": "0"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10023" )
        self.assertEqual(data["message"],"event status is not available")
    """发布会id已经达到limit人数"""
    def test_add_guest_event_id(self):
        json = {
            "event_id": "1",
            "realname": "张三",
            "phone": "15210001000",
            "email": "152100@qq.com",
            "sign": "0"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10024" )
        self.assertEqual(data["message"],"event number is full")
    """发布会已近结束"""
    def test_add_guest_event_id(self):
        json = {
            "event_id": "10",
            "realname": "张三",
            "phone": "15210001000",
            "email": "152100@qq.com",
            "sign": "0"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10025" )
        self.assertEqual(data["message"],"event has started")
    """S使用已签到的手机号再次进行签到"""
    def test_add_guest_event_id(self):
        json = {
            "event_id": "10",
            "realname": "张三",
            "phone": "15210001000",
            "email": "152100@qq.com",
            "sign": "0"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"10026" )
        self.assertEqual(data["message"],"'the event guest phone number repeat")
    """使用未签到的手机进行签到"""
    def test_add_guest_event_id(self):
        json = {
            "event_id": "10",
            "realname": "张三",
            "phone": "15210001001",
            "email": "152100@qq.com",
            "sign": "0"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data["status"],"200" )
        self.assertEqual(data["message"],"'add guest success")
 
class Sign(unittest.TestCase):
    def setUp(self):
        self.url = "http://127.0.0.1: 8000/api/sign/"
        self.headers = {'Content-Type': 'application/json'}
    """使用错误的请求方法"""
    def test_method_get(self):
        res = requests.get(self.url)
        data = res.json()
        self.assertEqual(data["status"],"404" )
        self.assertEqual(data["message"],"请求方法错误")
 
    """请求参数为空"""
    def test_method_get(self):
        json = {}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10020" )
        self.assertEqual(data["message"],"parameter is null")
    """请求参数 值为空"""
    def test_method_get(self):
        json = {"eid":"",
                "phone":""}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10021" )
        self.assertEqual(data["message"],"parameter error")
    """发布会id不存在"""
    def test_method_get(self):
        json = {"eid":9999,
                "phone":"15210001000"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10022" )
        self.assertEqual(data["message"],"event id null")
 
    """发布会状态是关闭的"""
    def test_method_get(self):
        json = {"eid":1,
                "phone":"15210001000"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10023" )
        self.assertEqual(data["message"],"event status is not available")
    """当前时间大于发布会时间,发布会已经开始了"""
    def test_method_get(self):
        json = {"eid":1,
                "phone":"15210001000"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10024" )
        self.assertEqual(data["message"],"event has started")
 
    """使用嘉宾表不存在的手机号测试"""
    def test_method_get(self):
        json = {"eid":1,
                "phone":"1521888888"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10025" )
        self.assertEqual(data["message"],"user phone null")
    """使用其他发布会手机号测试"""
    def test_method_get(self):
        json = {"eid":2,
                "phone":"1521888888"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10026" )
        self.assertEqual(data["message"],"user did not participate in the conference")
    """使用已签到的手机号测试"""
    def test_method_get(self):
        json = {"eid":1,
                "phone":"15210001000"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"10027" )
        self.assertEqual(data["message"],"user has sign in")
    """正常签到"""
    def test_method_get(self):
        json = {"eid":1,
                "phone":"15210001001"}
        res = requests.post(self.url,json,self.headers)
        data = res.json()
        self.assertEqual(data["status"],"200" )
        self.assertEqual(data["message"],"sign success")
 
 
if __name__ == '__main__':
    unittest.main()

以上是关于Django 2021年最新版教程35python+request+unittest 对Django接口进行测试的主要内容,如果未能解决你的问题,请参考以下文章

Django 2021年最新版教程1windows10+python3.9.5+pycharm2021.1.1+Django3.2.3新建一个web项目 教程

Django 2021年最新版教程36 python django runserver 后台运行

Django 2021年最新版教程20python for循环遍历queryset

Django 2021年最新版教程34python unittest 单元测试 覆盖率检测

Django 2021年最新版教程30django项目部署到华为云(nginx uWSGI mysql方式)

Django 2021年最新版教程26模板语言 前端if判断怎么用 实例