basic-auto-test/Commons/api_auto/batch_assert.py

40 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
# @Author : caiweichao
# @explain : 对于excel的结果进行批量校验
from Commons.util.json_util import JsonUtil
from Commons.util.logs import Log
class ApiAssert:
def __init__(self, checks: str, expecteds: str):
"""
:param checks: jsonpath
:param expecteds:期望值
"""
self.checks: list = checks.split("\n")
self.expecteds: list = expecteds.split("\n")
# 将全部对返回值进行校验
def assert_all_expected(self, response):
"""
全部字段校验
:param response:
:return:
"""
if len(self.checks) != len(self.expecteds):
return Exception("用例参数异常请检查!")
try:
for expected, check in dict(zip(self.expecteds, self.checks)).items():
if (expected := JsonUtil.jsonToOneValue(response, rule=expected)) == check:
Log.info(f"接口关键字段校验:{check} = {expected}")
else:
Log.error(f"接口关键字段校验未通过:{check} != {expected}")
return False
return True
except Exception:
Exception("校验异常请检查")
if __name__ == '__main__':
pass