python 接口自动化 - 面向对象思想对 Restful 类型的请求进行二次封装 - 写用例

项目目录结构:

├─api
├─core
└─testcase

  • api : 封装 python 接口
  • core:封装 Restful 类型的请求
  • testcase : 编写用例

根据 Requests 的源码进行二次封装

core/rest_client.py

import requests


class RestClient(object):

    def __init__(self, KeepLogin, api_root_url):
        self.KeepLogin = KeepLogin
        self.api_root_url = api_root_url

    def get(self, url, **kwargs):
        return self.request(url, "GET", **kwargs)

    def post(self, url, data = None, json = None, **kwargs):
        return self.request(url, "POST", data, json, **kwargs)

    def request(self, url, method, data = None, json = None, **kwargs):
        url = self.api_root_url + url
        if method == "GET":
            return self.KeepLogin.get(url, **kwargs)
        if method == "POST":
            return self.KeepLogin.post(url, data, json, **kwargs)




封装 python 接口

/api/schoolManageApi.py

from core.rest_client import RestClient


class SchoolManage(RestClient):
    def __init__(self, KeepLogin, api_root_url):
        super(SchoolManage, self).__init__(KeepLogin, api_root_url)

    def school_login(self, **kwargs):
        return self.get("/login/in", **kwargs)

    def school_add(self, **kwargs):
        return self.post("/school/manage/addSchoolInfo", **kwargs)




写测试用例

/testcase/testSmsCase.py

import random
import unittest
import requests
from api.schoolManageApi import SchoolManage

Userdict = {
    "account": "admin",
    "pwd": "660B8D2D5359FF6F94F8D3345698F88C"
}
Userdict_error = {
    "account": "admin",
    "pwd": "1660B8D2D5359FF6F94F8D3345698F88C"
}


class SMSLoginAndSchoolAdd(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.s = requests.session()
        self.url = "http://192.168.139.128:8080/recruit.students"
        self.KeepLogin = SchoolManage(self.s, self.url)
        self.KeepLogin.school_login(params=Userdict)

    @classmethod
    def tearDownClass(self):
        self.s.close()

    def test_01(self):
        print("测试用例01:正确用户名与密码,登陆成功")
        a = self.KeepLogin.school_login(params=Userdict)
        self.assertIn("退出", a.text)
        print("测试用例01通过")

    # def test_02(self):
    #     print("测试用例02:正确用户名与错误密码,登陆失败")
    #     a = self.KeepLogin.get("/login/in", Userdict_error)
    #     self.assertNotIn("退出", a.text)
    #     print("测试用例02通过")

    def test_03(self):
        print("测试用例03:在登陆状态下成功添加任意学校")
        # 一个随机学校名和一个随机备注
        SchoolNameRandom = random.randint(1, 1000000000)
        RemarkRandom = random.randint(1, 100000)

        # 新建学校post请求的body参数
        SchoolAddBody = {
            "schoolName": SchoolNameRandom,
            "listSchoolType[0].id": 2,
            "canRecruit": 1,
            "remark": RemarkRandom
        }
        a = self.KeepLogin.school_add(data=SchoolAddBody)
        print(a.text)
        # a.post("/school/manage/addSchoolInfo", data=SchoolAddBody)


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



后续还可以进行关键字的封装

现在在写用例的时候,还存在重复调用的地方

posted @ 2021-12-23 18:49  西瓜_皮  阅读(407)  评论(0编辑  收藏  举报