当前位置:网站首页>接口自动化2.0
接口自动化2.0
2022-08-10 05:35:00 【蓝尼亚】
整体思路如下

针对公司的项目做了些个性化处理:
一、数据处理
1.excel增加是否跑脚本列
有些接口调用比较麻烦:接口A依赖接口B、C、D,而A接口经常调用,其他3个接口很少用,接口A可以拎出单独写


2.接口依赖
接口依赖及处理:
类级别依赖(可以绑定到类属性)、方法级别(绑定到对象属性)、测试函数级别(动态绑定到类属性)
替换数据主要分为2大类:
(1)自定义数据
可以作为一个类的属性,需要该值,用反射获取属性
(2)依赖第三方接口
A依赖B接口返回参数。B返回值可以绑定到类属性/对象属性中,用反射获取对象性
def replace_variables(case_str, objects, type='vary'): # , imei=None, token=None
"""
替换变量和常量
:param case_str: 用例字符串
:param objects: 类名
:param type: vary:用例之间的依赖数据;constant:依赖配置
:return:
"""
flag = '#' if type == 'vary' else r'&'
# 找出需要替换的字符串
re_list = re.findall(f'{flag}.*?{flag}', case_str)
# 替换字符串
for item in list(set(re_list)):
key = item[1:-1]
case_str = case_str.replace(item, getattr(objects, key))
return case_str
二、测试环境常见问题处理
1.发送请求时,测试环境不稳定
发送请求出现异常/403,重试3次
import time
import requests
from common import logger
# requests.post(json=)
def req(url, headers, method='post', if_print=True, **kargs):
"""
发请求:如果出现异常会循环请求|响应为403,会循环请求3次
:param url:
:param headers:
:param method:
:param res_type: 响应类型
:param kargs: 请求其他参数:dict
:return:
"""
i = 0
while i < 3:
try:
logger.debug('============URL============')
logger.debug(url)
logger.debug('============method============\n{}'.format(method))
logger.debug('============headers============\n{}'.format(headers))
if if_print is True:
for key, value in kargs.items():
logger.debug('============{}============\n{}'.format(key.lower(), value))
logger.debug('============请求时间============\n{}'.format(time.strftime('%Y-%m-%d %H:%M:%S')))
res = getattr(requests, method)(url=url, headers=headers, **kargs)
logger.debug('============响应状态码============\n{}'.format(res.status_code))
logger.debug('============响应结果============\n{}'.format(res.text))
if res.status_code == 403:
logger.info('响应状态码为403')
i += 1
continue
except requests.exceptions.RequestException as e:
logger.error(e)
logger.info('错误次数:{}'.format(i))
i += 1
else:
return res
2.正式环境和测试环境跑自动化
测试环境和正式环境只有域名不一样,所以配置的时候,可以域名和接口名分开配置,方便取值

3.保持登录态
web端经常登录态失效,可以把脚本部署到服务器中,每分钟请求一次,保持登录态
边栏推荐
- STM32单片机OLED经典2048游戏单片机小游戏
- LeetCode 2011.执行操作后的变量值(简单)
- 51单片机BH1750智能补光灯台灯光强光照恒流源LED控制系统
- 【烘焙】肉松蛋糕卷
- pytorch-09. Multi-classification problem
- PyTorch之模型定义
- pytorch-09.多分类问题
- Convolutional Neural Network (CNN) for mnist handwritten digit recognition
- LeetCode refers to the offer 21. Adjust the order of the array so that the odd numbers are in front of the even numbers (simple)
- LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
猜你喜欢
随机推荐
卷积神经网络(CNN)实现服装图像分类
PyTorch 之 可视化网络架构
The submenu of the el-cascader cascade selector is double-clicked to display the selected content
机器学习——聚类——商场客户聚类
LeetCode 94. Inorder Traversal of Binary Trees (Simple)
Consensus calculation and incentive mechanism
win12 modify dns script
微信小程序--模板与设置WXML
pytorch-08. Load dataset
51单片机智能蓝牙APP加油站火灾预警安防防控报警监控系统MQ2DHT11
String common methods
STM32F407ZG 串口通信+固定帧头帧尾传输数据帧
【笔记】集合框架体系 Collection
STM32单片机OLED经典2048游戏单片机小游戏
Flutter Package 插件开发
51单片机手动自动智能窗户窗帘控制系统手动自动定时
el-dropdown drop-down menu style modification, remove the small triangle
大端以及小端以及读寄存器习惯
LeetCode 162.寻找峰值(中等)
51单片机智能远程遥控温控PWM电风扇系统红外遥控温度速度定时关机









