5. 自定义函数
机制
- 有些特殊参数生成规则, 如md5加解密等, 则可在此处定义
- 定义之后使用
${方法名()}
调用无参函数,${方法名(参数名)}
调用有参函数
hooks.py
# hooks.py
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
@project: apiAutoTest
@file: hooks.py
@author: zy7y
@time: 2021/2/27
@site: https://cnblogs.com/zy7y
@github: https://github.com/zy7y
@gitee: https://gitee.com/zy7y
@desc: 扩展方法, 2021/02/27
关于exec执行python代码可查阅资料:https://python3-cookbook.readthedocs.io/zh_CN/latest/c09/p23_executing_code_with_local_side_effects.html
"""
import time
def get_current_highest():
"""获取当前时间戳"""
return int(time.time())
def set_token(token: str):
"""设置token,直接返回字典"""
return {"Authorization": token}
def sum_data(a, b):
"""计算函数"""
return a + b
请求路径使用自定义函数¶
用例标题 | 是否跳过 | 请求头 | 请求地址 | 请求方式 | 入参关键字 | 上传文件 | 请求数据 | 提取参数 | 后置SQL | 预期结果 |
---|---|---|---|---|---|---|---|---|---|---|
登录 | FALSE | {"timer": "${get_current_highest()}"} | login/${get_current_highest()} | post | data | {"username": "admin", "password": ""} | {"msg":"$.meta.msg"} | select * from apis; | {"$.meta.status":400} |
请求数据处理:
假设基准地址使用的是
dev
:http://www.ysqorz.top:8888/api/private/v1/
假设当前时间戳为: 1621422609
最终请求地址为: http://www.ysqorz.top:8888/api/private/v1/login/1621422609
请求数据使用自定义函数¶
请求头使用自定义函数¶
请求头数据处理:
config.yaml
中request_header
配置如下:
# 基准的请求头信息
request_headers:
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
{
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
"timer": "1621422609"
}