python+request+unittest 接口测试练习

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()

  

posted @ 2018-10-23 17:05  studylady  阅读(1864)  评论(0编辑  收藏  举报