完善log接口测试返回数据验证方法

This commit is contained in:
byb11
2021-04-27 17:11:26 +08:00
parent cdf67995b7
commit 7d7b3beeb1
2 changed files with 84 additions and 47 deletions

View File

@@ -35,13 +35,13 @@ logtest
${EndTime1} Get Substring ${EndTime} \ -4 ${EndTime1} Get Substring ${EndTime} \ -4
${StartTime} add time to date ${EndTime} -01:00:00 ${StartTime} add time to date ${EndTime} -01:00:00
${StartTime1} Get Substring ${StartTime} \ -4 ${StartTime1} Get Substring ${StartTime} \ -4
${filter} Set Variable common_log_id=238347398589845504 OR common_log_id=238347398522859520 ${filter} Set Variable common_log_id=238734003578214400
${responsebody} loglistverify ${Logurl} ${Schemaurl} ${token} ${StartTime1} ${EndTime1} ${logType} ${filter} ${responsebody} loglistverify ${Logurl} ${Schemaurl} ${token} ${StartTime1} ${EndTime1} ${logType} ${filter}
${targetdict} create list common_log_id = 237434463277422592 ${targetdict} create list common_log_id = 238734003578214400
log ${responsebody} log ${responsebody}
${strlist} FieldValidation ${responsebody} ${targetdict} ${strlist} FieldValidation ${responsebody} ${targetdict}
log ${strlist} log ${strlist}
Assertresults ${strlist} # Assertresults ${strlist}

View File

@@ -1,51 +1,90 @@
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
def FieldValidation(responsedict, targetlist): def FieldValidation(responsedict, targetlist):
responselist = responsedict["data"]["list"] responselist = responsedict["data"]["list"]
strlist = [] strlist = []
if responselist: if responselist:
#循环返回数据列表 # 循环返回数据列表
sum = 1 sum = 1
for response in responselist: for response in responselist:
# 循环目地列表 # 循环目地列表
for t in targetlist: for t in targetlist:
#将目的根据空格分割成列表 “key值”“判断条件”“value值” # 将目的根据空格分割成列表 “key值”“判断条件”“value值”
target=t.split(" ") target = t.split(" ")
print("target",target) #获取json串中所有的key返回列表
#判断目的条件的Key在数据中是否存在 responsekeys = getKeys(response)
if target[0] in response: # 判断目的条件的Key在数据中是否存在
if target[0] in responsekeys:
#targetkey 判断的字段
targetkey = target[0] targetkey = target[0]
#判断条件 # 判断条件
conditions=target[1] conditions = target[1]
#的Value # 返回数据中对应key的Value列表
# 对应Key返回数据的Value responsevaluelist = getjsonvalue(response,target[0])
responsevalue = response[target[0]] for responsevalue in responsevaluelist:
print(targetkey,conditions,responsevalue) #判断value值是否为列表转化为字符串
print(len(target)) if isinstance(responsevalue, list):
if len(target) == 3: responsevalue=str(responsevalue)
targetvalue=target[2] if len(target) == 3:
p=conditional(conditions,responsevalue,targetkey,sum,targetvalue) targetvalue = target[2]
strlist.append(p) p = conditional(conditions, responsevalue, targetkey, sum, targetvalue)
elif len(target) == 2: strlist.append(p)
p=conditional(conditions, responsevalue, targetkey, sum) elif len(target) == 2:
strlist.append(p) p = conditional(conditions, responsevalue, targetkey, sum)
strlist.append(p)
else: else:
str2 = "返回数据第" + str(sum) + "组数据中不存在该字段:" + target[0] str2 = "返回数据第" + str(sum) + "组数据中不存在该字段:" + target[0]
print(str2) print(str2)
strlist.append(str2) strlist.append(str2)
sum+=1 sum += 1
else: else:
str3 = "返回数据中无数据" str3 = "返回数据中无数据"
strlist.append(str3) strlist.append(str3)
Assertresults(strlist)
return strlist return strlist
def conditional(conditions,value2,targetkey,sum,value=None): def getjsonvalue(json_data, key_name):
str1="" '''获取到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 conditions == "=":
if value != value2: if value != value2:
str1= "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符"
if conditions == "!=": if conditions == "!=":
if value == value2: if value == value2:
@@ -69,35 +108,35 @@ def conditional(conditions,value2,targetkey,sum,value=None):
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "in": if conditions == "in":
value=value.split(",") value = value.split(",")
if value2 not in value: if value2 not in value:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "notin": if conditions == "notin":
value=value.split(",") value = value.split(",")
if value2 in value: if value2 in value:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
if conditions == "like": if conditions == "like":
left= value[0] left = value[0]
right=value[-1] right = value[-1]
if left == "%" and right == "%": if left == "%" and right == "%":
value=value[1:len(value)-1] value = value[1:len(value) - 1]
if value not in value2: if value not in value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
elif left == "%" and right != "%": elif left == "%" and right != "%":
v=len(value) v = len(value)
_value = value[1:] _value = value[1:]
_value2 = value2[-(v-1):] _value2 = value2[-(v - 1):]
print(_value,_value2) print(_value, _value2)
if _value != _value2: if _value != _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
elif left != "%" and right == "%": elif left != "%" and right == "%":
v=len(value) v = len(value)
_value = value[0:-1] _value = value[0:-1]
_value2=value2[0:v-1] _value2 = value2[0:v - 1]
if _value != _value2: if _value != _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
@@ -112,7 +151,7 @@ def conditional(conditions,value2,targetkey,sum,value=None):
elif left == "%" and right != "%": elif left == "%" and right != "%":
v = len(value) v = len(value)
_value = value[1:] _value = value[1:]
_value2 = value2[-(v-1):] _value2 = value2[-(v - 1):]
if _value == _value2: if _value == _value2:
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
@@ -130,12 +169,10 @@ def conditional(conditions,value2,targetkey,sum,value=None):
if conditions == "Empty": if conditions == "Empty":
if value2 != "": if value2 != "":
str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。"
return str1 return str1
def Assertresults(resultslist): def Assertresults(resultslist):
print(resultslist) print(resultslist)
for i in resultslist: for i in resultslist:
if i != "": if i != "":
assert 1==2 assert 1 == 2