'''
主脚本,用于串联各个py文件
'''
import logging,os,re
from common import request, opmysql, analyse, compare
from public import config
base_request= request.RequestInterface()
base_operationdb_interface= opmysql.OperationDbInterface()
try:
print("开始接口自动化程序,请选择操作类型(0|执行用例;1|导出测试结果)")
value_input = input('请输入操作类型:')
while not re.search(r'^[0-1]$',value_input):
print ("请输入正确的操作类型(0|执行用例;1|导出测试结果)")
value_input = str(input('请输入操作类型:'))
else:
if value_input=='0':
print("您输入的是:0|执行测试用例")
module_execute=base_operationdb_interface.select_all("SELECT value_config from config_total where key_config='exe_setup' and status=1")
if len(module_execute['data'])!=0 and module_execute['code']=='0000':
for module_execute_one in module_execute['data']:
temp_module_execute=eval(module_execute_one['value_config'])
for temp_name_interface,condition in temp_module_execute.items():
print('############开始执行接口:%s############\n' %(temp_name_interface))
temp_level_check=condition['level_check']
temp_level_exe=tuple(condition['level_exe'])
data_case_interface=base_operationdb_interface.select_all("select * from case_interface where case_status=1 and name_interface='%s' and exe_level in %s" %(temp_name_interface,temp_level_exe))
if data_case_interface['code']=='0000' and len(data_case_interface['data'])!=0:
for temp_case_interface in data_case_interface['data']:
id_case=str(temp_case_interface['id'])
url_interface=temp_case_interface['url_interface']
headerdata=eval(temp_case_interface['header_interface'])
param_interface=temp_case_interface['params_interface']
type_interface=temp_case_interface['exe_mode']
result_http_response=base_request.http_request(interface_url=url_interface,headerdata=headerdata,interface_param=param_interface,request_type=type_interface)
print("接口地址:%s\n请求参数:%s\n返回包数据:%s" %(url_interface,param_interface,result_http_response))
base_operationdb_interface.op_sql("UPDATE case_interface set result_interface='%s' where id=%s" %(result_http_response['data'],id_case))
if result_http_response['code']=='0000' and len(result_http_response['data'])!=0:
for child_level_check in temp_level_check:
base_compare= compare.CompareParam(temp_case_interface)
if child_level_check in (0,):
result_compare_code=base_compare.compare_code(result_http_response['data'])
print('用例编号:%s|检查级别:关键参数值|接口名称:%s|提示信息:%s\n' %(id_case,temp_name_interface,result_compare_code['message']))
elif child_level_check in [1]:
result_compare_params_complete=base_compare.compare_params_complete(result_http_response['data'])
print('用例编号:%s|检查级别:参数完整性|接口名称:%s|提示信息:%s\n' %(id_case,temp_name_interface,result_compare_params_complete['message']))
elif child_level_check in [2]:
pass
elif child_level_check in (3,):
pass
else:
print('用例编号:%s|接口名称:%s|检查级别错误:%s\n' %(id_case,temp_name_interface,child_level_check))
elif len(result_http_response['data'])==0:
print('用例编号:%s|接口名称:%s|错误信息:接口返回数据为空\n' %(id_case,temp_name_interface))
else:
print('用例编号:%s|接口名称:%s|错误信息:%s\n' %(id_case,temp_name_interface,result_http_response['message']))
elif len(data_case_interface['data'])==0:
print('接口名称:%s|错误信息:获取用例数据为空,请检查用例\n' %(temp_name_interface))
else:
print('接口名称:%s|错误信息:获取用例数据失败|错误信息:%s\n' %(temp_name_interface,data_case_interface['message']))
print('############结束执行接口:%s############\n' %(temp_name_interface))
else:
print('错误信息:待执行接口获取失败|错误信息:%s' %module_execute['message'])
elif value_input=='1':
print('您输入的是:1|导出测试结果,请注意查看目录:%s' %(config.src_path+'\\report'))
names_export=base_operationdb_interface.select_one("select value_config from config_total where status=1 and key_config='name_export'")
if names_export['code']=='0000' and len(names_export['data']['value_config'])!=0:
temp_export=eval(names_export['data']['value_config'])
test_analyse_data= analyse.AnalyseData()
result_export=test_analyse_data.export2excel(temp_export)
print (result_export['message'])
print ("导出失败接口列表:%s\n" %result_export['data'])
else:
print("请检查配置表数据正确性,当前值为:%s\n" %names_export['data'])
except Exception as error:
print("系统出现异常:%s" %error)
logging.basicConfig(filename = config.src_path + '\log\syserror.log', level = logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
logger.exception(error)
input('Press Enter to exit...')