python⾃动⽣成接⼝测试⽤例_swagger⾃动⽣成接⼝测试⽤
importsys
sys.path.append('D:\Interface_framework_Beauty')importrequestsimportosfrom common.operation_excel import Write_excel #写⼊excel模块
from common.logger import Log #打印⽇志模块
from common.processing_json import write_data #写⼊json⽂件模块
from common.difference importdiff_excel, diff_jsonfrom common importread_config
title_list=[]
old_excel_path=os.path.abspath(
os.path.dirname(os.path.dirname(__file__))) + '\\case_generate' + '\\data_old' +
'\\demo_api.xlsx'excel_path=os.path.abspath(
os.path.dirname(matlab text
os.path.dirname(__file__))) + '\\case_generate' + '\\data_new' + '\\demo_api.xlsx' #case path
old_json_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + '\\case_generate' + '\\data_old'json_path= os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + '\\case_generate' + '\\data_new'
classAnalysisJson:"""swagger⾃动⽣成测试⽤例"""
def __init__(self, url_json):
self.url_json=url_json
r= (self.url_json + '/v2/api-docs?group=sign-api').json()
self.title= r['info']['description']
write_data(r,'{}.json'.format(self.title))
self.interface_params ={}
self.log=Log()
python解析json文件
self.num = 1 #case id
globaltitle_list, json_pathifself.check_data(r):
self.json_path=os.path.abspath(
os.path.dirname(
os.path.dirname(__file__))) + '\\case_generate' + '\\data_new' + '\\{}_data.json'.format(
self.title)#json file path,执⾏多个url的情况,区分⽣成的json⽂件
self.data = r['paths'] #paths中的数据是有⽤的
title_list.append(self.title)defcheck_data(self, r):"""检查返回的数据是否是dict"""
if notisinstance(r, dict):
self.log.info('swagger return json error.')returnFalseelse:returnTruedefretrieve_data(self):"""主函数"""
globalbody_name, methodfor k, v inself.data.items():
method_list=[]for _k, _v inv.items():
interface={}if not _v['deprecated']: #接⼝是否被弃⽤
degrademethod_list.append(_k)
api= k #api地址
if len(method_list) > 1: #api地址下的请求⽅式不⽌⼀个的情况
for i inrange(len(method_list)):
body_name= place('/', '_') + '_' * i #json⽂件对应参数名称,excel中body名称
method = method_list[-1] #请求⽅式 同⼀个api地址,不同请求⽅式
else:
body_name= place('/', '_')
method=_k
self.interface_ieve_excel(_v, interface, api)else:
self.log.info('interface path: {}, case name: {}, is deprecated.'.format(k, _v['description']))break
ifself.interface_params:
write_data(self.interface_params, self.json_path)#参数写⼊json⽂件
defretrieve_excel(self, _v, interface, api):"""解析参数,拼接为dict--准备完成写⼊excel的数据"""parameters= _v.get('parameters') #未解析的参数字典
if not parameters: #确保参数字典存在
parameters ={}
case_name = _v['summary'] #接⼝名称
tags = _v['tags'][0] #标签名称
params_dict = ieve_params(parameters) #处理接⼝参数,拼成dict形式
gopro splice下载if params_dict and parameters != {}: #单个或多个参数
interface['row_num'] = w #写⼊excel时的所在⾏
interface['id'] = 'test_' + str(self.num) #case id
interface['tags'] = tags #标签名称
interface['name'] =case_name
_type = 'json' #参数获取⽅式
interface['method'] = method #请求⽅式
interface['url'] = self.url_json + api #拼接完成接⼝url
interface['headers'] = 'yes' #是否传header
interface['body'] =body_name
interface['type'] =_type
self.num+= w+= 1
self.interface_params[body_name] =params_dict
self.write_excel(interface, excel_path)#参数写⼊excel
else: #没有参数
_type = 'data_old'interface['name'] =case_name
interface['row_num'] =w
interface['id'] = 'test_' +str(self.num)
interface['tags'] =tags
interface['method'] =method
双老太婆原唱完整版
interface['url'] = self.url_json +api
interface['headers'] = 'yes'interface['body'] = ''interface['type'] =_type
self.num+= w+= 1self.interface_params[body_name]=params_dict
self.write_excel(interface, excel_path)returnself.interface_paramsdefretrieve_params(self, parameters):"""处理参数,转为dict"""params= ''_in= ''
for each inparameters:
_in+= ('in') + '\n' #参数传递位置
java数据结构树
params += ('name') + '\n' #参数
_in = _in.strip('\n')
_in_list= _in.split('\n')
params= params.strip('\n')
params_list= params.split('\n')
del_list=py()for i inrange(len(_in_list)):if _in_list[i] == 'header':
ve(del_list[i])#只保存在body传的参数
test_list =py()
params_dict= dict(zip(params_list, test_list)) #把list转为dict
returnparams_dictdefwrite_excel(self, interface, filename):"""把dict中的值写⼊对应的excel⾏中"""wt=Write_excel(filename, self.title)try:
wt.write(interface['row_num'], 1, interface['id'])
wt.write(interface['row_num'], 2, interface['tags'])
wt.write(interface['row_num'], 3, interface['name'])
wt.write(interface['row_num'], 4, interface['method'])
wt.write(interface['row_num'], 5, interface['url'])
wt.write(interface['row_num'], 7, interface['headers'])
wt.write(interface['row_num'], 8, interface['body'])
wt.write(interface['row_num'], 10, interface['type'])
self.log.info('Interface case id {},write to excel file successfully!'.format(interface['id']))exceptException as e:
self.log.info('Failure of interface use case to write to excel file! error:{}\n'.format(e))return
defdiff_file():"""对⽐⽂件"""
globaltitle_listfor title intitle_list:
diff_excel(old_excel_path, excel_path, title)
diff_json(os.path.join(old_json_path,'{}_data.json'.format(title)),
os.path.join(json_path,'{}_data.json'.format(title)), title)if __name__ == '__main__':
url= ate_url.split(',')for i inurl:#url_json = i + '/v2/api-docs?group=sign-api' # json swagger url地址AnalysisJson(i).retrieve_data()
diff_file()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。