mirror of https://gitee.com/a529548204/apitest.git
205 lines
8.4 KiB
Python
205 lines
8.4 KiB
Python
# coding:utf-8
|
|
|
|
import logging
|
|
import time
|
|
|
|
import allure
|
|
import jsonpath
|
|
|
|
from scripts.dataBase import MYSQL
|
|
from scripts.log import Log
|
|
from scripts.readYamlFile import ini_yaml
|
|
|
|
Log()
|
|
|
|
|
|
def assert_json(data, key=None, value=None, asserttype="KEY"):
|
|
"""
|
|
json返回值断言
|
|
:param data: 返回数据
|
|
:param key:
|
|
:param value:
|
|
:param asserttype: "KEY" OR "VALUE" OR "KEY-VALUE"
|
|
:return:
|
|
"""
|
|
if asserttype == "KEY":
|
|
try:
|
|
assert data.get(key) is not None
|
|
with allure.step("判断JSON返回值KEY是否存在"):
|
|
allure.attach(name="期望存在KEY", body=str(key))
|
|
allure.attach(name='实际data', body=str(data))
|
|
logging.info(
|
|
"断言通过, 存在key'{}', value为'{}'.".format(key, data.get(key)))
|
|
except Exception:
|
|
logging.error("断言未通过, 不存在key:'{}'.".format(key))
|
|
raise
|
|
elif asserttype == "VALUE":
|
|
try:
|
|
rs_values = list(data.values())
|
|
with allure.step("判断JSON返回值VALUE是否存在"):
|
|
allure.attach(name="期望VALUE", body=str(value))
|
|
allure.attach(name='实际VALUE', body=str(rs_values))
|
|
assert value in rs_values
|
|
|
|
logging.info("断言通过, 存在value为'{}', key为'{}'.".format(value, list(data.keys())[
|
|
list(data.values()).index(value)]))
|
|
except AssertionError:
|
|
logging.error(
|
|
"断言未通过, 不存在类型为'{}'的value:''{}''.".format(type(value), value))
|
|
raise
|
|
elif asserttype == "KEY-VALUE":
|
|
try:
|
|
|
|
with allure.step("判断JSON返回值是否存在KEY-VALUE"):
|
|
allure.attach(name="期望KEY-VALUE", body=str({key: value}))
|
|
allure.attach(name='实际KEY-VALUE', body=str(data))
|
|
if key in data:
|
|
assert value == data[key]
|
|
else:
|
|
logging.error("断言未通过, 不存在key:'{}'.".format(key))
|
|
raise AssertionError
|
|
logging.info("断言通过, 存在键值对key为'{}',value为'{}'.".format(list(data.keys())[
|
|
list(data.values()).index(value)], value))
|
|
except AssertionError:
|
|
logging.error(
|
|
"断言未通过, 不存在key为'{}',类型为'{}'的value:''{}''.".format(key, type(value), value))
|
|
raise
|
|
else:
|
|
logging.error("断言类型错误, 请选择断言类型.")
|
|
|
|
|
|
def assert_text(hope_res, real_res):
|
|
"""
|
|
文本判断
|
|
:param hope_res: 期望结果
|
|
:param real_res: 实际结果
|
|
:return:
|
|
"""
|
|
if isinstance(hope_res["jsonpath"], list):
|
|
for h_res in hope_res["jsonpath"]:
|
|
if jsonpath.jsonpath(real_res, h_res["path"]):
|
|
r_res = jsonpath.jsonpath(real_res, h_res["path"])[0]
|
|
if h_res["asserttype"] == "==":
|
|
try:
|
|
with allure.step("json断言判断相等"):
|
|
allure.attach(name="期望结果", body=str(h_res))
|
|
allure.attach(name='实际实际结果', body=str(r_res))
|
|
assert r_res == h_res["value"]
|
|
logging.info("json断言通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
|
except AssertionError:
|
|
logging.error("json断言未通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
|
raise
|
|
elif h_res["asserttype"] == "!=":
|
|
try:
|
|
with allure.step("json断言判断不等"):
|
|
allure.attach(name="json期望结果", body=str(h_res))
|
|
allure.attach(name='json实际实际结果', body=str(r_res))
|
|
assert r_res != h_res["value"]
|
|
logging.info("json断言通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
|
except AssertionError:
|
|
logging.error("json断言未通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
|
raise
|
|
elif h_res["asserttype"] == "in":
|
|
r_res = str(r_res)
|
|
try:
|
|
with allure.step("json断言判断包含"):
|
|
allure.attach(name="期望结果", body=str(h_res))
|
|
allure.attach(name='实际实际结果', body=str(r_res))
|
|
assert r_res in h_res["value"]
|
|
logging.info("json断言通过, 期望结果{0}, 实际结果{1}".format(h_res, real_res))
|
|
except AssertionError:
|
|
logging.error("json断言未通过, 期望结果{0}, 实际结果{1}".format(h_res, real_res))
|
|
raise
|
|
else:
|
|
raise TypeError("asserttype方法错误")
|
|
else:
|
|
raise ValueError("获取json值失败,请检查jsonpath")
|
|
|
|
|
|
def assert_time(hope_res, real_res):
|
|
hope_time = hope_res["time"]
|
|
try:
|
|
with allure.step("判断响应时间"):
|
|
allure.attach(name="期望响应时间", body=str(hope_time))
|
|
allure.attach(name='实际响应时间', body=str(real_res))
|
|
assert real_res <= hope_time
|
|
logging.info("time断言通过, 期望响应时间{0}, 实际响应时间{1}".format(hope_time, real_res))
|
|
except AssertionError:
|
|
logging.error("请求响应时间过长, 期望时间{0}, 实际时间{1}".format(hope_time, real_res))
|
|
raise
|
|
|
|
|
|
def assert_sql(hope_res, real_res):
|
|
if isinstance(hope_res["sqlassert"], list):
|
|
db = MYSQL()
|
|
for h_res in hope_res["sqlassert"]:
|
|
h_sql = h_res["sql"]
|
|
for i in h_res["datas"]:
|
|
if jsonpath.jsonpath(real_res, i["path"]):
|
|
r_res = jsonpath.jsonpath(real_res, i["path"])[0]
|
|
t1 = time.time()
|
|
datas = db.run_sql(h_sql)
|
|
try:
|
|
with allure.step("断言判断相等"):
|
|
allure.attach(name="期望结果", body=str(datas))
|
|
allure.attach(name='实际实际结果', body=str(r_res))
|
|
assert r_res == datas[0][i["name"]]
|
|
logging.info("sql断言通过, 期望结果{0}, 实际结果{1},sql耗时{2:.5f}秒".format(datas, r_res,time.time()-t1))
|
|
except AssertionError:
|
|
logging.error("sql断言未通过, 期望结果{0}, 实际结果{1},sql耗时{2:.5f}秒".format(datas, r_res,time.time()-t1))
|
|
raise
|
|
else:
|
|
raise ValueError("获取json值失败,请检查jsonpath")
|
|
|
|
def asserting(hope_res, real_res,re_time=None):
|
|
if hope_res["jsonpath"]:
|
|
assert_text(hope_res, real_res)
|
|
if hope_res["sqlassert"]:
|
|
assert_sql(hope_res, real_res)
|
|
if hope_res["time"]:
|
|
assert_time(hope_res, re_time)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
j = {'code': 0, 'msg': 'Success.', 'data': {
|
|
'token': 'eyJ1c2VyX2lkIjoxOTYsInVzZXJuYW1lIjoiZmluc2lvdCIsImV4cCI6MTYzMzc2MDg0NCwiZW1haWwiOiIifQ',
|
|
'id': 196,'username': '123'}}
|
|
# hp = {
|
|
# "jsonpath": [
|
|
#
|
|
# ],
|
|
# "sqlassert": [
|
|
# {
|
|
# "datas": [
|
|
# {
|
|
# "path": "$.data.id",
|
|
# "name": "id"
|
|
# },
|
|
# {
|
|
# "path": "$.data.username",
|
|
# "name": "username"
|
|
# },
|
|
# ],
|
|
# "sql": "select * from saas.user where username = '123' ",
|
|
# },
|
|
# {
|
|
# "datas": [
|
|
# {
|
|
# "path": "$.data.id",
|
|
# "name": "id"
|
|
# },
|
|
# {
|
|
# "path": "$.data.username",
|
|
# "name": "username"
|
|
# },
|
|
# ],
|
|
# "sql": "select * from saas.user where username = '44324' ",
|
|
# }
|
|
# ],
|
|
# "time": None
|
|
# }
|
|
hp = ini_yaml("loginData.yml")
|
|
print(hp)
|
|
# asserting(hp, j,23)
|