封装接口返回值校验(关键参数校验和参数完整性校验)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import operateDB
import request
op = operateDB.OperateDB()
class Compare(object):
global result1
# 初始化数据(期望值等)
def __init__(self, param):
self.except_param = param["code_to_compare"]
self.except_code = param["code_expect"]
self.id_case = param["id"]
self.except_params = param["params_to_compare"]
self.result_params_name = []
# 关键参数比较
def compare_code(self, result_interface):
try:
if json.loads(result_interface):
resultDic = json.loads(result_interface)
# 判断关键参数是否存在
if self.except_param in resultDic:
# 判断关键参数值是否与预期值相同
if str(resultDic[self.except_param]) == str(self.except_code):
result = {"code": 00, "message": "参数值相同", "data": []}
op.modifyOne("UPDATE case_interface set code_actual='%s',result_code_compare=%s where id=%s"
% (resultDic[self.except_param], 0, self.id_case))
else:
result = {"code": 10, "message": "参数值不同", "data": []}
op.modifyOne("UPDATE case_interface set code_actual='%s',result_code_compare=%s where id=%s"
% (resultDic[self.except_param], 1, self.id_case))
else:
result = {"code": 20, "message": "返回包中不存在此参数", "data": []}
op.modifyOne("UPDATE case_interface set result_code_compare=%s where id=%s"
% (2, self.id_case))
else:
result = {"code": 30, "message": "返回包格式错误", "data": []}
op.modifyOne("UPDATE case_interface set result_code_compare=%s where id=%s"
% (3, self.id_case))
except Exception as e:
result = {'code': '999', 'message': u'关键字参数值比较异常', 'data': []}
op.modifyOne("UPDATE case_interface set result_code_compare=%s where id=%s"
% (999, self.id_case))
print e.message
print result
return result
# 获取接口返回参数并去重(递归实现)
def get_compare_param(self, result_interface):
try:
# print type(result_interface)
if isinstance(result_interface, (str, unicode)) and json.loads(result_interface):
res = json.loads(result_interface)
self.get_compare_param(res)
elif isinstance(result_interface, dict):
for key, value in result_interface.iteritems():
self.result_params_name.append(key)
if isinstance(value, dict):
self.get_compare_param(value)
elif isinstance(value, list):
for param in value:
self.get_compare_param(param)
else:
continue
else:
pass
except Exception as e:
return {"code": 999, "message": "处理返回包参数异常", "data": []}
print e.message
# set去除重复且有序
return {'code': '00', 'message': u'成功', 'data': list(set(self.result_params_name))}
# 参数完整性比较
def compare_param(self, result_interface):
try:
actual_params = self.get_compare_param(result_interface)
print actual_params["data"]
if actual_params["code"] == "00":
actual_params_list = actual_params["data"]
if set(actual_params_list).issubset(self.except_params):
result = {"code": 00, "message": "参数完整性比较一致", "data": []}
op.modifyOne('UPDATE case_interface set params_actual="%s",result_params_compare=%s where id="%s"'
% (actual_params_list, 0, self.id_case))
else:
result = {"code": 00, "message": "实际需要参数不在返回值中", "data": []}
op.modifyOne('UPDATE case_interface set params_actual="%s",result_params_compare=%s where id="%s"'
% (actual_params_list, 1, self.id_case))
except Exception as e:
result = {"code": 999, "message": "处理返回包参数异常", "data": []}
print e.message
# print result
return result
if __name__ == "__main__":
param = op.queryOne("select * from case_interface limit 1")
re = request.Request()
response = re.httpRequest(param["data"]["exe_mode"], param["data"]["url_interface"],
eval(param["data"]["header_interface"]),
param["data"]["params_interface"])
com = Compare(param["data"])
# com.compare_code(response["data"])
# com.get_compare_param(response["data"])
com.compare_param(response["data"])