接口校验优化

This commit is contained in:
caiweichao 2022-06-20 17:06:32 +08:00
parent 4a05cebf08
commit 21d4c0b64a
3 changed files with 61 additions and 40 deletions

View File

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
# @Author : caiweichao
# @explain : 接口返回值校验
import allure
from Commons.util.json_util import JsonUtil
from Commons.util.logs import Log
import jsonschema
class ApiAssert:
@staticmethod
def assert_all_expected(jsonPath: str, check: str, response: str):
"""
全部字段校验,将全部对返回值进行校验
:param jsonPath:jsonpath
:param check: 期望值
:param response:
:return:
"""
if len(jsonPath := jsonPath.split("\n")) != len(check := check.split("\n")):
return Exception("用例参数异常请检查!")
try:
for k, v in dict(zip(jsonPath, check)).items():
if (k := JsonUtil.jsonToOneValue(response, rule=k)) == v:
Log.info(f"\n接口关键字段校验:{v} = {k}")
else:
Log.error(f"接口关键字段校验未通过:{v} != {k}")
return False
return True
except Exception:
Exception("校验异常请检查")
@staticmethod
def assert_jsonschema(response, schema):
"""
jsonschema与实际结果断言方法
:param response: 实际响应结果
:param schema: jsonschema
:return:
"""
try:
Log.info("jsonschema校验开始")
jsonschema.validate(instance=response, schema=schema)
Log.info("jsonschema校验通过")
allure.step(f"jsonschema校验通过")
except jsonschema.exceptions.ValidationError or jsonschema.exceptions.SchemaError as e:
Log.error(f"jsonschema校验失败报错信息为 {e}")
allure.step(f"jsonschema校验失败,报错信息为: {e}")
raise e
if __name__ == '__main__':
jsonxx = [{'pm10': 24, 'city': '珠海', 'time': '2016-10-23 13:00:00'}]
json_schema = {'type': 'array', 'items':
{'type': 'object',
'properties': {'pm10': {'type': 'number', }, 'city': {'type': 'string', 'enum': ['珠海', '深圳']},
'time': {'type': 'string'}}}}
ApiAssert.assert_jsonschema(jsonxx, json_schema)

View File

@ -1,39 +0,0 @@
# -*- 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

View File

@ -33,7 +33,7 @@
#### 更新日志 #### 更新日志
- 20220619 - 20220619
1. 接口自动化支持多关键字校验按照下图在excel中维护用例写一个参数就换行 1. 接口自动化支持多关键字校验按照下图在excel中维护用例写一个参数就换行
2. Commons/api_auto/batch_assert.py 这个类支持接口参数多字段校验一个失败即为失败 2. Commons/api_auto/assert_method.py 支持接口多字段校验
1. [![Xj28pV.jpg](https://s1.ax1x.com/2022/06/19/Xj28pV.jpg)](https://imgtu.com/i/Xj28pV) 1. [![Xj28pV.jpg](https://s1.ax1x.com/2022/06/19/Xj28pV.jpg)](https://imgtu.com/i/Xj28pV)