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 FieldValidation(responsedict, targetlist): responselist = responsedict["data"]["list"] strlist = [] if responselist: #循环返回数据列表 sum = 1 for response in responselist: # 循环目地列表 for t in targetlist: #将目的根据空格分割成列表 (“key值”,“判断条件”,“value值”) target=t.split(" ") print("target",target) #判断目的条件的Key在数据中是否存在 if target[0] in response: targetkey = target[0] #判断条件 conditions=target[1] #目的Value # 对应Key返回数据的Value responsevalue = response[target[0]] print(targetkey,conditions,responsevalue) print(len(target)) 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) else: str2 = "返回数据第" + str(sum) + "组数据中不存在该字段:" + target[0] print(str2) strlist.append(str2) sum+=1 else: str3 = "返回数据中无数据" strlist.append(str3) return strlist def Assertresults(resultslist): print(resultslist) for i in resultslist: if i != "": assert 1==2