直接看代码把==
原理其实就是转为对应格式,写入---到testcaseinfo里
#!/usr/bin/python
import requests
import json,time
import pymysql
def importswagger():
#配置数据库连接
db = pymysql.connect("127.0.0.1", "root", "123456", "hrun")
cursor = db.cursor()
sql = "INSERT INTO testcaseinfo(create_time,update_time,type,name,belong_project,include,author,request,belong_module_id) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s')"
#获取swagger接口json数据
# page = requests.get("http://ccc/v2/api-docs")
# pagevalue = page.content.decode("utf-8")
# dict1 = json.loads(pagevalue)
f=open('api-docs.json',encoding='utf-8')
pagevalue=f.read()
dict1 = json.loads(pagevalue)
keys = list(dict1['paths'].keys())
#所有的url下的键和值
path = dict1.get('paths')
baseurl = "https://ccc.cn"
message = "cls"
#取出每个url每个method下对应的值
for key in keys:
param = []
data = {}
request = {}
test = {}
interface = {}
url = ''
summary = ''#接口名称
body = path[str(key)]
#print(body.keys())#dict_keys(['get', 'head', 'post', 'put', 'delete', 'options', 'patch'])
#print(len(body.keys()))
url = baseurl + "/" +message + key
for i in body.keys():
method = i.upper()
# print(method)
if method in ['GET', 'POST', 'PUT', 'DELETE']:
print('************')
summary = body[i]['summary'][0:15]+str(method)
summary = summary.replace('/','')
parametersbody = body[i]['parameters']
for v in parametersbody:
value =v['name']
param.append(value)
data[value] = ''
#封装request
request['url'] = url
request['method'] = method
request['data'] = data
request['headers'] = ''
# request['name'] = summary
#封装test
test['request'] = request
test['name'] = summary
print("*"*50)
print(test)
#封装整句
interface['test'] = test
#将字典转化成字符串
interfacestr = str(interface)
print(interfacestr)
#将每个分号前加上转义符
cc = pymysql.escape_string(interfacestr)
create_timee = time.strftime("%Y-%m-%d %X")
cursor.execute(sql % (create_timee, create_timee, '1', str(summary), 'test000', '[]', 'ditto', cc , '1'))
db.commit()
print("------------")
if __name__ == '__main__':
importswagger()
网友评论