当前位置: 首页 > news >正文

python自动化测试之统一请求封装及通过文件实现接口关联

一、接口文档怎么看?

http://www.aaa.com/api.php?s=index/index&application=app&application_client_type=weixi
n&token=tokenvalue&ajax=ajax
参数解释:
  • http 协议
  • www.aaa.com IP和端口
  • api.php 接口的地址
  • s=index/index 接口名称以 控制器/方法 组合、如 index/index
  • application=app 公共参数,取值:web或app
  • application_client_type=weixin 公共参数

  • token=token 公共参数
  • ajax=ajax 异步请求
能够请求成功的URL应该是这样的:
http:// 101.34.221.219:8010/ api.php?
s=index/index&application=app&application_client_type=h5&token=token&ajax=ajax
nginx:80
tomcat:8080

二、接口自动化实战

看上一篇笔记写的自动化实战项目

可以发现一些问题:
1.发现有很多重复的冗余的代码
2.多个py文件之间的接口是不能自动化的关联cookie

接口自动化测试框架封装技术点:统一请求封装

目的:
1.去重重复的冗余的代码
2.跨py文件实现通过一个sess来自动关联有cookie关联的接口。
3.设置统一的公共参数,统一的文件处理,统一的异常处理,统一的日志监控,统一的用例校验等
等。。。

实现步骤:

1.在commons目录下创建封装一个同意请求的类

2.改造之前写的接口请求代码,

将TestShopxo.sess.request()用封装的方法替换RequestUtil().sed_all_request()【红色框】

3.将公共参数在RequestUtil().sed_all_request()提供【蓝色框】,将接口自动化脚本中的重复代码删除【黄色框】

import requests

#统一请求封装
class RequestUtil:
    sess = requests.session()
    #使用同一个session会话的模块
    def sed_all_request(self,**kwargs):
        total_params = {
            "application": "app",
            "application_client_type": "h5"
        }
        for key,value in kwargs.items():
            if key =="params":
                kwargs["params"].update(total_params)
            elif key =="files":
                for file_key,file_value in value.items():
                    value[file_key] = open(file_value,"rb")

        #发送请求
        res = RequestUtil.sess.request(**kwargs)
        return res
import jsonpath
import requests

from commons.request_util import RequestUtil
from commons.yaml_util import write_yaml, read_yaml


class TestShopxo:
    # token = ""
    # sess = requests.session()
    #首页列表接口
    def test_start_list(self):
        method = "post"
        url ="http://shop-xo.hctestedu.com/index.php"
        parmas = {
            # "application" : "app",
            # "application_client_type" : "h5",
            "s" : "api/index/index"
        }
        res = RequestUtil().sed_all_request(method=method, url=url, params=parmas)
        print(res.json())


        # 登陆接口
    def test_login_shopxo(self):
        method = "post"
        url ="http://shop-xo.hctestedu.com/index.php"
        params = {
            # "application": "app",
            # "application_client_type": "h5",
            "s": "api/user/login"
        }
        json = {
            "accounts": "yut2001",
            "pwd": "123456",
            "verify": "rib5",
            "type": "username"
        }
        res = RequestUtil().sed_all_request(method=method, url=url, params=params, json=json)

        print(res.json())
    # 提取token
        TestShopxo.token = jsonpath.jsonpath(res.json(), "$.data.token")[0]
        data = {"token":jsonpath.jsonpath(res.json(), "$.data.token")[0]}
        write_yaml(data)

    def test_order_list(self):
            method = "post"
            url = "http://shop-xo.hctestedu.com/index.php"
            params = {
                # "application": "app",
                # "application_client_type": "h5",
                "s": "api/order/index",
                "token": read_yaml("token")
            }
            json = {
                "page": "1",
                "keywords": "",
                "status": "-1",
                "is_more": "1"
            }
            res = RequestUtil().sed_all_request(method=method, url=url, params=params, json=json)
            print(res.json())
#商品详情页
    def test_order_detail(self):
        method = "post"
        url ="http://shop-xo.hctestedu.com/index.php"
        params = {
            # "application": "app",
            # "application_client_type": "h5",
            "s": "api/goods/detail"
        }
        json = {
            "goods_id": "12"
        }
        res = RequestUtil().sed_all_request(method=method, url=url, params=params, json=json)

        print(res.json())

五、接口自动化测试框架封装技术点:接口关联封装

目的:
1.统一管理接口关联的中间变量(最好是接口执行之后能够看到中间变量的值,利于拍错)
2.解决多个py文件中间的中间变量关联的问题
方案:
接口关联封装是通过一个yaml文件来保存中间变量,让后通过读,写以及清空来处理这些中间变量
所有用例请求之前去清空还是所有用例请求之后去清空?==== 之前

实现步骤:

1.创建一个extract.yaml文件,用来临时存储中间变量

2.调整yaml工具类
3.创建 conftest.py文件实现对yaml工具类的调用

 

相关文章:

  • 传感器篇(一)——深度相机
  • 第一章嵌入式系统概论考点10互联网
  • 基于Spring Security 6的OAuth2 系列之十五 - 高级特性--客户端认证方式
  • 机器学习实战之基于随机森林的气温预测
  • 设计模式——职责链模式
  • Maven 中的 `<dependencyManagement>` 标签及其高级用法
  • centos7安装vscode
  • MySql从入门到精通
  • qt 控件的焦点事件
  • 共享设备管理难?MDM助力Kiosk模式一键部署
  • P2704 [NOI2001] 炮兵阵地
  • 血压高吃哪些水果比较好喵?
  • VM ubuntu20.04 虚拟机与主机之间不能互相复制的解决
  • Deepseek R1模型本地化部署+API接口调用详细教程:释放AI生产力
  • 云原生时代的后端开发:架构、工具与最佳实践
  • 6 Flink Table 和相关概念
  • TCP可靠传输的ARQ协议
  • 20250214在ubuntu20.04下使用obs studio录制外挂的1080p的USB摄像头【下载安装】
  • vm vitualbox和主机ssh连接,使用net 和仅主机网卡连接
  • Python爬虫抓取数据时,如何设置请求头?
  • 前四个月人民币贷款增加10.06万亿元,4月末M2余额同比增长8%
  • 第十二届警博会在京开幕:12个国家和地区835家企业参展
  • 法治课|争议中的“行人安全距离”于法无据,考量“注意义务”才更合理
  • 尊严的代价:新加坡福利体系下的价值困境
  • 睡觉总做梦是睡眠质量差?梦到这些事,才要小心
  • 中国工程院院士、国医大师石学敏逝世