【Python】如何优雅的完成自动化测试用例请求与断言封装
前言
记录Json断言在工作中的应用进阶。
直接使用
很早以前写过一篇博客,记录当时获取一个多级json中指定key的数据:
#! /usr/bin/python
# coding:utf-8
"""
@author:Bingo.he
@file: get_target_value.py
@time: 2017/12/22
"""
def get_target_value(key, dic, tmp_list):
"""
:param key: 目标key值
:param dic: JSON数据
:param tmp_list: 用于存储获取的数据
:return: list
"""
if not isinstance(dic, dict) or not isinstance(tmp_list, list): # 对传入数据进行格式校验
return 'argv[1] not an dict or argv[-1] not an list '
if key in dic.keys():
tmp_list.append(dic[key]) # 传入数据存在则存入tmp_list
for value in dic.values(): # 传入数据不符合则对其value值进行遍历
if isinstance(value, dict):
get_target_value(key, value, tmp_list) # 传入数据的value值是字典,则直接调用自身
elif isinstance(value, (list, tuple)):
_get_value(key, value, tmp_list) # 传入数据的value值是列表或者元组,则调用_get_value
return tmp_list
def _get_value(key, val, tmp_list):
for val_ in val:
if isinstance(val_, dict):
get_target_value(key, val_, tmp_list) # 传入数据的value值是字典,则调用get_target_value
elif isinstance(val_, (list, tuple)):
_get_value(key, val_, tmp_list) # 传入数据的value值是列表或者元组,则调用自身
优化
后来在写用例生成过程中,发现重复的断言描述信息较多,大多数数据返回其实都是标准的json,所以将整个返回的json放到断言数据中:
continue_run_flag = True
def assert_json(base, juge, contain=(), reg=()):
# 返回值,不符合预期时会设置为False
flag = True
for key, value in base.items():
# 不进行断言的数据不进一步处理
if key not in juge:
continue
if key in COMMON_RE_CHECK:
if not re.match(COMMON_RE_CHECK[key], base[key]):
flag = False
logger.error("字段[{}]使用通用的正则匹配,不符合预期:预期正则匹配【{}】== 【{}】".format(key, COMMON_RE_CHECK[key], juge[key]))
else:
logger.warning("字段【{}】使用通用字段的正则匹配, 符合预期".format(key))
continue
if key in contain:
if str(value) not in juge[key]:
flag = False
logger.error("字段[{}]不符合预期:预期【{}】包含 【{}】".format(key, juge[key], value))
continue
logger.info("字段[{}]符合预期:预期[{}] 包含 [{}]".format(key, juge[key], value))
continue
if key in reg:
if not re.match(juge[key], value):
flag = False
logger.error("字段[{}]不符合预期:预期正则匹配【{}】== 【{}】".format(key, value, juge[key]))
continue
logger.info("字段[{}]断言成功:预期[{}]== 实际[{}]".format(key, value, juge[key]))
continue
if isinstance(value, str) or isinstance(value, int):
if juge[key] != value:
logger.error("字段[{}]不符合预期:预期【{}】!= 实际【{}】".format(key, value, juge[key]))
flag = False
continue
elif isinstance(value, dict):
assert_json(value, juge[key], contain=contain, reg=reg)
elif isinstance(value, list):
for i, v in enumerate(value):
if isinstance(value, str) or isinstance(value, int):
if v != juge[key][i]:
logger.error("字段[{}]不符合预期:预期【{}】!= 实际【{}】".format(key, value, juge[key]))
else:
logger.info("字段[{}]断言成功:预期[{}]== 实际[{}]".format(key, value, juge[key]))
elif isinstance(value, dict):
assert_json(value[i], juge[key][i], contain=contain, reg=reg)
else:
assert_json(value[i], juge[key][i], contain=contain, reg=reg)
else:
pass
logger.info("字段[{}]符合预期: 预期[{}]== 实际[{}]".format(key, value, juge[key]))
# 失败是否继续执行,默认为TRUE
if not continue_run_flag:
assert flag
return flag
调用:
rsp = requests.get('http://localhost:8800/get', params={"name": "bingo", "age": 18}).json()
assert_json(rsp, {
"args": {
"name": "bingo"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Cache-Control": "max-age=259200",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.27.1",
"X-Amzn-Trace-Id": r"Root=\d-\w{8}-\w{24}"
},
"req_param": [
{
"name": "bingo",
"age": "18"
}
],
"origin": r"",
"url": "http://httpbin.org/get?name=bingo"
},
contain=(), reg=("X-Amzn-Trace-Id", "origin",))
日志效果:
2022-05-05 14:25:49.967 | INFO | __main__:assert_json:173 - 字段[name]符合预期: 预期[bingo]== 实际[bingo]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[args]符合预期: 预期[{'name': 'bingo'}]== 实际[{'name': 'bingo'}]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[Accept]符合预期: 预期[*/*]== 实际[*/*]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[Accept-Encoding]符合预期: 预期[gzip, deflate]== 实际[gzip, deflate]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[Cache-Control]符合预期: 预期[max-age=259200]== 实际[max-age=259200]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[Host]符合预期: 预期[httpbin.org]== 实际[httpbin.org]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[User-Agent]符合预期: 预期[python-requests/2.27.1]== 实际[python-requests/2.27.1]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:149 - 字段[X-Amzn-Trace-Id]断言成功:预期[Root=1-62734553-430db0707e1a3656043cd165]== 实际[Root=\d-\w{8}-\w{24}]
2022-05-05 14:25:49.968 | INFO | __main__:assert_json:173 - 字段[headers]符合预期: 预期[{'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'max-age=259200', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=1-62734553-430db0707e1a3656043cd165'}]== 实际[{'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'max-age=259200', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=\\d-\\w{8}-\\w{24}'}]
2022-05-05 14:25:49.969 | WARNING | __main__:assert_json:133 - 字段【origin】使用通用字段的正则匹配, 符合预期
2022-05-05 14:25:49.969 | INFO | __main__:assert_json:173 - 字段[age]符合预期: 预期[18]== 实际[18]
2022-05-05 14:25:49.969 | INFO | __main__:assert_json:173 - 字段[name]符合预期: 预期[bingo]== 实际[bingo]
2022-05-05 14:25:49.969 | INFO | __main__:assert_json:173 - 字段[req_param]符合预期: 预期[[{'age': '18', 'name': 'bingo'}]]== 实际[[{'name': 'bingo', 'age': '18'}]]
2022-05-05 14:25:49.969 | INFO | __main__:assert_json:173 - 字段[url]符合预期: 预期[http://httpbin.org/get?name=bingo]== 实际[http://httpbin.org/get?name=bingo]
封装
将方法简单封装到调用类中:
class HttpBin:
def __init__(self):
self.continue_run_flag = True # 失败是否继续执行
self.base_url = 'http://localhost:8800'
self.base_param = {"local_class": self.__class__.__name__}
def get(self, param):
path = "/get"
param.update(self.base_param)
self.rsp = requests.get(self.base_url + path, params=param)
self.ans = self.rsp.json()
logger.info(json.dumps(self.rsp.json(), indent=4))
return self
def set(self, param):
path = "/set"
param.update(self.base_param)
self.rsp = requests.get(self.base_url + path, params=param)
self.ans = self.rsp.json()
logger.info(json.dumps(self.rsp.json(), indent=4))
return self
def assert_statusCode(self, result_code):
"""
:param result_code: 包含关系断言
:return: bool <= self.rsp.resultinfo
"""
# 返回值,不符合预期时会设置为False
flag = True
if int(result_code) != self.rsp.status_code:
logger.error(f"返回状态码[result_code]不符合预期:预期【{result_code}】!= 实际【{self.rsp.status_code}】")
flag = False
else:
logger.info(f"返回状态码[result_code]符合预期:预期【{result_code}】!= 实际【{self.rsp.status_code}】")
if not self.continue_run_flag:
assert flag
return self
def assert_json_body(self, base, juge, contain=(), reg=()):
...
用例调用
# 如果仅仅断言状态码
HttpBin().get({"name": "bingo", "age": 18}).assert_statusCode(200)
# 级连调用多个接口,使用同一个初始化数据
HttpBin().get({"name": "bingo", "age": 18}).assert_statusCode(200).\
set({"name": "he", "age": 18}).assert_statusCode(200).assert_json_body(
juge={
"args": {
"name": "bingo"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Cache-Control": "max-age=259200",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.27.1",
"X-Amzn-Trace-Id": r"Root=\d-\w{8}-\w{24}"
},
"req_param": [
{
"name": "he",
"age": "18"
}
],
"origin": r"",
"url": "http://httpbin.org/set?name=bingo"
}, contain=(), reg=("X-Amzn-Trace-Id", "origin",)
)
运行效果:
2022-05-05 19:39:36.951 | INFO | __main__:assert_statusCode:53 - 返回状态码[result_code]符合预期:预期【200】!= 实际【200】
2022-05-05 19:39:36.951 | INFO | __main__:assert_json_body:117 - 字段[name]符合预期: 预期[bingo]== 实际[bingo]
2022-05-05 19:39:36.951 | INFO | __main__:assert_json_body:117 - 字段[args]符合预期: 预期[{'name': 'bingo'}]== 实际[{'name': 'bingo'}]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:117 - 字段[Accept]符合预期: 预期[*/*]== 实际[*/*]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:117 - 字段[Accept-Encoding]符合预期: 预期[gzip, deflate]== 实际[gzip, deflate]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:117 - 字段[Cache-Control]符合预期: 预期[max-age=259200]== 实际[max-age=259200]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:117 - 字段[Host]符合预期: 预期[httpbin.org]== 实际[httpbin.org]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:117 - 字段[User-Agent]符合预期: 预期[python-requests/2.27.1]== 实际[python-requests/2.27.1]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:93 - 字段[X-Amzn-Trace-Id]断言成功:预期[Root=1-62734553-430db0707e1a3656043cd165]== 实际[Root=\d-\w{8}-\w{24}]
2022-05-05 19:39:36.952 | INFO | __main__:assert_json_body:117 - 字段[headers]符合预期: 预期[{'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'max-age=259200', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=1-62734553-430db0707e1a3656043cd165'}]== 实际[{'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'max-age=259200', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.27.1', 'X-Amzn-Trace-Id': 'Root=\\d-\\w{8}-\\w{24}'}]
2022-05-05 19:39:36.953 | WARNING | __main__:assert_json_body:77 - 字段【origin】使用通用字段的正则匹配, 符合预期
2022-05-05 19:39:36.953 | INFO | __main__:assert_json_body:117 - 字段[age]符合预期: 预期[18]== 实际[18]
2022-05-05 19:39:36.953 | INFO | __main__:assert_json_body:117 - 字段[name]符合预期: 预期[he]== 实际[he]
2022-05-05 19:39:36.953 | INFO | __main__:assert_json_body:117 - 字段[req_param]符合预期: 预期[[{'age': '18', 'local_class': 'HttpBin', 'name': 'he'}]]== 实际[[{'name': 'he', 'age': '18'}]]
2022-05-05 19:39:36.953 | INFO | __main__:assert_json_body:117 - 字段[url]符合预期: 预期[http://httpbin.org/set?name=bingo]== 实际[http://httpbin.org/set?name=bingo]
小结
- 可以作为独立函数使用,也可以和所有的接口一起封装,灵活度、复用度高
- 用例极简且逻辑清晰
- 支持断言失败,用例继续执行(开关控制),方便一次性发现所有的差异
- 极易编写的统一断言(从日志获取轻松获取)
- 用例运行日志,断言数据字段清晰明确
- 可统一化处理新版本修改字段,无需修改每个用例
- 支持正则匹配
- 支持**包含匹配 **
- 支持错误码直接断言
- 集中初始化原始常用数据,不同业务适配简单,重写初始化方法即可
进阶
上面的方法虽然使用日志的方法记录了所有的差异,但是面对大json对比的时候,很难直接标记出具体的差异位置。在做现网引流对比测试的时候就出现了这样的需求,从现网拉取的账户数据可能存在几百个子账户,每个子账户有20多个属性字段,怎么准确标记他们在新旧系统的写操作后不一致的情况成为了一个小卡点。
话不多说,思路:利用列表可变特性和生成器关键字yield特性递归分解json,生成一个固定的数组,最终比较数组中的数据
代码:
def recurse(d, prefix=None, sep='.'):
if prefix is None:
prefix = []
for key, value in d.items():
if isinstance(value, dict):
yield from recurse(value, prefix + [key])
elif isinstance(value, list):
for i, v in enumerate(value):
if isinstance(v, dict):
yield from recurse(v, prefix + [key, f"${i}"])
# 兼容 包含数字的类型
elif isinstance(v, int) or isinstance(v, str):
yield sep.join(prefix + [key, str(value)]) # 会嵌套上value
else:
# print(key)
yield sep.join(prefix + [key, str(value)]) # 会嵌套上value
效果:
print(json.dumps(list(recurse({
"args": {
"name": "bingo"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Cache-Control": "max-age=259200",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.27.1",
"X-Amzn-Trace-Id": r"Root=\d-\w{8}-\w{24}"
},
"req_param": [
{
"name": "bingo",
"age": "18"
},
{
"name": "he",
"age": "19"
},
{
"name": "detector",
"age": "20"
}
],
"origin": r"",
"url": "http://httpbin.org/set?name=bingo"
})), indent=4))
# 输出
[
"args.name.bingo",
"headers.Accept.*/*",
"headers.Accept-Encoding.gzip, deflate",
"headers.Cache-Control.max-age=259200",
"headers.Host.httpbin.org",
"headers.User-Agent.python-requests/2.27.1",
"headers.X-Amzn-Trace-Id.Root=\\d-\\w{8}-\\w{24}",
"req_param.$0.name.bingo",
"req_param.$0.age.18",
"req_param.$1.name.he",
"req_param.$1.age.19",
"req_param.$2.name.detector",
"req_param.$2.age.20",
"origin.",
"url.http://httpbin.org/set?name=bingo"
]
总结
项目实践中总会遇到这样那样的需求,每个方法都有适用的场景,直接高效解决问题是第一要务。
- json查找数据
- json数据用例封装对比
- json数据转化
文中可能存在描述不正确,欢迎大神们指正补充!
感谢阅读,如果觉得对你有帮助,就在右下角点个赞吧,感谢!
合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。