This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
dongxiaoyan-tsg-autotest/04-CustomLibrary/Custometest/LogResponseVAL.py

179 lines
7.9 KiB
Python
Raw Normal View History

import jsonpath
# 1.说明:本方法用于对日志接口返回数据中的字段和数据进行判断
# 2.传入数据说明responsedict - 接口返回数据的json数据
# targetlist - 判断参数 可传入多个条件在robotfromwork中条件以Tab分隔 。参数格式为: 字段key_判断条件_数据value ,一个条件内各参数以空格分隔 例common_log_id = 238734003578214400
# 判断条件包括:= 、 != 、> 、< 、>= 、<= 、in 、notin 、like、 notlike 、notEmpty 、 empty 。
# 1其中notin、notlike、notEmpty传入时中间无空格
# 2notEmpty、empty 不传数据value
# 3 in 、notin 多个字段以逗号分隔 例 common_log_id notin 238734003578214400238734003578214402238734003578214403
2021-04-27 11:32:22 +08:00
def FieldValidation(responsedict, targetlist):
responselist = responsedict["data"]["list"]
strlist = []
if responselist:
# 循环返回数据列表
2021-04-27 11:32:22 +08:00
sum = 1
for response in responselist:
# 循环目地列表
for t in targetlist:
# 将目的根据空格分割成列表 “key值”“判断条件”“value值”
target = t.split(" ")
#获取json串中所有的key返回列表
responsekeys = getKeys(response)
# 判断目的条件的Key在数据中是否存在
if target[0] in responsekeys:
#targetkey 判断的字段
2021-04-27 11:32:22 +08:00
targetkey = target[0]
# 判断条件
conditions = target[1]
# 返回数据中对应key的Value列表
responsevaluelist = getjsonvalue(response,target[0])
for responsevalue in responsevaluelist:
#判断value值是否为列表转化为字符串
if isinstance(responsevalue, list):
responsevalue=str(responsevalue)
if len(target) == 3:
targetvalue = target[2]
p = conditional(conditions, responsevalue, targetkey, sum, targetvalue)
strlist.append(p)
elif len(target) == 2:
p = conditional(conditions, responsevalue, targetkey, sum)
strlist.append(p)
2021-04-27 11:32:22 +08:00
else:
str2 = "返回数据第" + str(sum) + "组数据中不存在该字段:" + target[0]
print(str2)
strlist.append(str2)
sum += 1
2021-04-27 11:32:22 +08:00
else:
str3 = "返回数据中无数据"
strlist.append(str3)
Assertresults(strlist)
2021-04-27 11:32:22 +08:00
return strlist
def getjsonvalue(json_data, key_name):
'''获取到json中任意key的值,结果为list格式'''
keyvalue = jsonpath.jsonpath(json_data, '$..{key_name}'.format(key_name=key_name))
# key的值不为空字符串或者为empty用例中空固定写为empty返回对应值否则返回empty
return keyvalue
def getKeys(data):
# 获取json串中所有的key
keysAll_list = []
def getkeys(data): # 遍历json所有key
if (type(data) == type({})):
keys = data.keys()
for key in keys:
value = data.get(key)
if (type(value) != type({}) and type(value) != type([])):
keysAll_list.append(key)
elif (type(value) == type({})):
keysAll_list.append(key)
getkeys(value)
elif (type(value) == type([])):
keysAll_list.append(key)
for para in value:
if (type(para) == type({}) or type(para) == type([])):
getkeys(para)
else:
keysAll_list.append(para)
getkeys(data)
return keysAll_list
# 对传入的数据根据条件进行判断
def conditional(conditions, value2, targetkey, sum, value=None):
str1 = ""
if conditions == "=":
if value != value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符"
if conditions == "!=":
if value == value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
print(str1)
if conditions == ">":
if int(value2) <= int(value):
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "<":
if int(value2) >= int(value):
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == ">=":
if int(value2) < int(value):
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "<=":
if int(value2) > int(value):
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "in":
value = value.split(",")
if value2 not in value:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "notin":
value = value.split(",")
if value2 in value:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "like":
left = value[0]
right = value[-1]
if left == "%" and right == "%":
value = value[1:len(value) - 1]
if value not in value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
elif left == "%" and right != "%":
v = len(value)
_value = value[1:]
_value2 = value2[-(v - 1):]
print(_value, _value2)
if _value != _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
elif left != "%" and right == "%":
v = len(value)
_value = value[0:-1]
_value2 = value2[0:v - 1]
if _value != _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "notlike":
left = value[0]
right = value[-1]
if left == "%" and right == "%":
value = value[1:len(value) - 1]
if value in value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
elif left == "%" and right != "%":
v = len(value)
_value = value[1:]
_value2 = value2[-(v - 1):]
if _value == _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
elif left != "%" and right == "%":
v = len(value)
_value = value[0:-1]
_value2 = value2[0:v - 1]
if _value == _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "notEmpty":
if value2 == "":
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "Empty":
if value2 != "":
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
return str1
def Assertresults(resultslist):
print(resultslist)
for i in resultslist:
if i != "":
assert 1 == 2