欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > python自动化测试之统一请求封装及通过文件实现接口关联

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

2025/2/26 3:40:05 来源:https://blog.csdn.net/qq_48584557/article/details/145597439  浏览:    关键词: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 requestsfrom commons.request_util import RequestUtil
from commons.yaml_util import write_yaml, read_yamlclass 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())# 提取tokenTestShopxo.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工具类的调用

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词