Python接口自动化测试——four

前言——request请求方法封装

一、为什么封装请求方法

在第一篇有说到接口测试就是模拟用户端向服务端发送请求的测试,既然要模拟要肯定需要我们去发送请求,而在现实工作中需要调用的接口肯定很多,因此需要将请求方法封装方便后续的调用

二、封装的方法

1.引入库

代码如下(示例):

import requests
import json
from common.Log import logger
from common.heapcase import Heapcase

2.对常用的请求进行封装(get,post,delete,put)

代码如下(示例):

class RunMain():
    def get(self, url, data, headers):
        try:
            r = requests.get(url, params=data, headers=headers)
            r.encoding = 'utf-8'
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败!", str(e))

    def post(self, url, data, headers):
        try:
            r = requests.post(url, data=data, headers=headers)
            r.encoding = 'utf-8'
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败!", str(e))

    def post_json(self, url, data, headers):
        try:
            data = json.dumps(data)
            r = requests.post(url, data=data, headers=headers)
            r.encoding = 'utf-8'
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败!", str(e))

    def put(self, url, data, headers):
        try:
            data = json.dumps(data)
            r = requests.put(url, data=data, headers=headers)
            r.encoding = "utf-8"
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败", str(e))

    def delete(self, url, data, headers):
        try:
            data = json.dumps(data)
            r = requests.delete(url, data=data, headers=headers)
            r.encoding = "utf-8"
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败", str(e))

3.确定用例调用哪个请求方法

每个用例的请求方式都不同,怎么去确定我们用例的请求方法呢?这就要和上一篇的内容有关,再用例管理的Excel表格里我们会去写一个method,然后通过读取这个值来调用对应的方法。所以这里我们还需要定义一个方法,就是根据method值来调用对应的请求方式

def run_main(self, method, url, data, headers):
        result = None
        if method == 'post':
            result = self.post(url, data, headers)
            #logger.info(str(result))#这个可以先不看,后面会讲解到
        elif method == 'get':
            result = self.get(url, data, headers)
            #logger.info(str(result))#这个可以先不看,后面会讲解到
        elif method == 'post_json':
            result = self.post_json(url, data, headers)
            logger.info(str(result))
        elif method == "put":
            result = self.put(url, data, headers)
             #logger.info(str(result))#这个可以先不看,后面会讲解到
        elif method == "delete":
            result = self.delete(url, data, headers)
             #logger.info(str(result))#这个可以先不看,后面会讲解到
        else:
            print("method值错误!!!")
             #logger.info(str(result))#这个可以先不看,后面会讲解到
        return result

4.完整代码如下

import requests
import json
from common.Log import logger
from common.heapcase import Heapcase


class RunMain():
    def get(self, url, data, headers):
        try:
            r = requests.get(url, params=data, headers=headers)
            r.encoding = 'utf-8'
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败!", str(e))

    def post(self, url, data, headers):
        try:
            r = requests.post(url, data=data, headers=headers)
            r.encoding = 'utf-8'
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败!", str(e))

    def post_json(self, url, data, headers):
        try:
            data = json.dumps(data)
            r = requests.post(url, data=data, headers=headers)
            r.encoding = 'utf-8'
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败!", str(e))

    def put(self, url, data, headers):
        try:
            data = json.dumps(data)
            r = requests.put(url, data=data, headers=headers)
            r.encoding = "utf-8"
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败", str(e))

    def delete(self, url, data, headers):
        try:
            data = json.dumps(data)
            r = requests.delete(url, data=data, headers=headers)
            r.encoding = "utf-8"
            json_r = r.json()
            print("Test执行结果:", json_r)
            return json_r
        except BaseException as e:
            print("请求失败", str(e))

    def run_main(self, method, url, data, headers):
        result = None
        if method == 'post':
            result = self.post(url, data, headers)
            logger.info(str(result))
        elif method == 'get':
            result = self.get(url, data, headers)
            logger.info(str(result))
        elif method == 'post_json':
            result = self.post_json(url, data, headers)
            logger.info(str(result))
        elif method == "put":
            result = self.put(url, data, headers)
            logger.info(str(result))
        elif method == "delete":
            result = self.delete(url, data, headers)
            logger.info(str(result))
        else:
            print("method值错误!!!")
            logger.info('method值错误!!!')
        return result


if __name__ == '__main__':
	RunMain().run_main("method","url",headers)

总结

以上就是本次的讲解,如果大家有什么其他的想法可以一起学习,感谢…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值