2020-08-03 16:15:03 +08:00
|
|
|
|
#!/usr/bin/env/python3
|
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
@project: apiAutoTest
|
|
|
|
|
@author: zy7y
|
|
|
|
|
@file: __init__.py.py
|
|
|
|
|
@ide: PyCharm
|
|
|
|
|
@time: 2020/7/31
|
2020-11-19 00:49:26 +08:00
|
|
|
|
"""
|
2020-11-19 23:35:49 +08:00
|
|
|
|
import json
|
2020-11-20 23:55:22 +08:00
|
|
|
|
import re
|
|
|
|
|
|
2020-11-19 23:35:49 +08:00
|
|
|
|
import allure
|
|
|
|
|
|
2020-11-19 00:49:26 +08:00
|
|
|
|
from jsonpath import jsonpath
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extractor(obj: dict, expr: str = '.') -> object:
|
|
|
|
|
"""
|
2020-11-21 17:40:59 +08:00
|
|
|
|
根据表达式提取字典中的value,表达式, . 提取字典所有内容, $.case 提取一级字典case, $.case.data 提取case字典下的data
|
2020-11-19 00:49:26 +08:00
|
|
|
|
:param obj :json/dict类型数据
|
|
|
|
|
:param expr: 表达式, . 提取字典所有内容, $.case 提取一级字典case, $.case.data 提取case字典下的data
|
|
|
|
|
$.0.1 提取字典中的第一个列表中的第二个的值
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
result = jsonpath(obj, expr)[0]
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f'提取不到内容,丢给你一个错误!{e}')
|
|
|
|
|
result = None
|
|
|
|
|
return result
|
|
|
|
|
|
2020-11-19 23:35:49 +08:00
|
|
|
|
|
2020-11-20 23:55:22 +08:00
|
|
|
|
def rep_expr(content: str, data: dict, expr: str = '&(.*?)&') -> str:
|
|
|
|
|
"""从请求参数的字符串中,使用正则的方法找出合适的字符串内容并进行替换
|
|
|
|
|
:param content: 原始的字符串内容
|
|
|
|
|
:param data: 在该项目中一般为响应字典,从字典取值出来
|
|
|
|
|
:param expr: 查找用的正则表达式
|
|
|
|
|
return content: 替换表达式后的字符串
|
|
|
|
|
"""
|
|
|
|
|
for ctt in re.findall(expr, content):
|
|
|
|
|
content = content.replace(f'&{ctt}&', str(extractor(data, ctt)))
|
2021-01-27 14:19:29 +08:00
|
|
|
|
# 解决运算问题,实现+ -等常规数学运算, 用例书写格式{"uid":eval`&$.pid&+1`} 如果需要是字符串{"uid":eval`&$.pid&+1`}
|
|
|
|
|
for e in re.findall('eval`(.*)`', content):
|
|
|
|
|
content = content.replace(f'eval`{e}`', str(eval(e)))
|
2020-11-20 23:55:22 +08:00
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
2020-11-19 23:35:49 +08:00
|
|
|
|
def convert_json(dict_str: str) -> dict:
|
|
|
|
|
"""
|
|
|
|
|
:param dict_str: 长得像字典的字符串
|
|
|
|
|
return json格式的内容
|
|
|
|
|
"""
|
2020-11-20 20:09:12 +08:00
|
|
|
|
try:
|
|
|
|
|
if 'None' in dict_str:
|
|
|
|
|
dict_str = dict_str.replace('None', 'null')
|
|
|
|
|
elif 'True' in dict_str:
|
|
|
|
|
dict_str = dict_str.replace('True', 'true')
|
|
|
|
|
elif 'False' in dict_str:
|
|
|
|
|
dict_str = dict_str.replace('False', 'false')
|
2020-11-20 23:55:22 +08:00
|
|
|
|
dict_str = json.loads(dict_str)
|
2020-11-20 20:09:12 +08:00
|
|
|
|
except Exception as e:
|
2020-11-20 23:55:22 +08:00
|
|
|
|
if 'null' in dict_str:
|
|
|
|
|
dict_str = dict_str.replace('null', 'None')
|
|
|
|
|
elif 'true' in dict_str:
|
|
|
|
|
dict_str = dict_str.replace('true', 'True')
|
2021-01-18 12:16:54 +08:00
|
|
|
|
elif 'false' in dict_str:
|
2020-11-20 23:55:22 +08:00
|
|
|
|
dict_str = dict_str.replace('false', 'False')
|
|
|
|
|
dict_str = eval(dict_str)
|
|
|
|
|
logger.error(e)
|
|
|
|
|
return dict_str
|
2020-11-19 23:35:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def allure_title(title: str) -> None:
|
|
|
|
|
"""allure中显示的用例标题"""
|
|
|
|
|
allure.dynamic.title(title)
|
|
|
|
|
|
|
|
|
|
|
2020-11-20 23:55:22 +08:00
|
|
|
|
def allure_step(step: str, var: str) -> None:
|
2020-11-19 23:35:49 +08:00
|
|
|
|
"""
|
2020-11-20 23:55:22 +08:00
|
|
|
|
:param step: 步骤及附件名称
|
2020-11-19 23:35:49 +08:00
|
|
|
|
:param var: 附件内容
|
|
|
|
|
"""
|
|
|
|
|
with allure.step(step):
|
2020-11-22 22:20:33 +08:00
|
|
|
|
allure.attach(json.dumps(var, ensure_ascii=False, indent=4), step, allure.attachment_type.TEXT)
|