---整体更新一波---
1.实际工作中,因为要动手输入的地方比较多,自动生成的异常接口用例感觉用处不大,就先去掉了,只保留了正常的;
2.接口有改动的,如果开发人员没有及时告知或没有详细告知,会增加一些不必要的麻烦,所以增加了文件对比功能;
目录:
case_generate.py
import sys
sys.path.append('D:\Interface_framework_Beauty') import requests import os from common.operation_excel import Write_excel # 写入excel模块
from common.logger import Log # 打印日志模块
from common.processing_json import write_data # 写入json文件模块
from common.difference import diff_excel, diff_json from common import read_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(
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'
class AnalysisJson: """swagger自动生成测试用例"""
def \_\_init\_\_(self, url_json):
self.url_json = url_json
r = requests.get(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()
self.row = 2 # 写入excel起始行数
self.num = 1 # case id
global title\_list, json\_path if self.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) def check_data(self, r): """检查返回的数据是否是dict"""
if not isinstance(r, dict):
self.log.info('swagger return json error.') return False else: return True def retrieve_data(self): """主函数"""
global body_name, method for k, v in self.data.items():
method_list = \[\] for \_k, \_v in v.items():
interface = {} if not _v\['deprecated'\]: # 接口是否被弃用
method_list.append(_k)
api = k # api地址
if len(method_list) > 1: # api地址下的请求方式不止一个的情况
for i in range(len(method_list)):
body_name = api.replace('/', '_') \+ '_' \* i # json文件对应参数名称,excel中body名称
method = method_list\[-1\] # 请求方式 同一个api地址,不同请求方式
else:
body_name = api.replace('/', '_')
method = _k
self.interface_params = self.retrieve\_excel(\_v, interface, api) else:
self.log.info('interface path: {}, case name: {}, is deprecated.'.format(k, _v\['description'\])) break
if self.interface_params:
write\_data(self.interface\_params, self.json_path) # 参数写入json文件
def retrieve\_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 = self.retrieve\_params(parameters) # 处理接口参数,拼成dict形式
if params_dict and parameters != {}: # 单个或多个参数
interface\['row_num'\] = self.row # 写入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 += 1 self.row += 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'\] = self.row
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 += 1 self.row += 1 self.interface\_params\[body\_name\] = params_dict
self.write\_excel(interface, excel\_path) return self.interface_params def retrieve_params(self, parameters): """处理参数,转为dict""" params = '' _in = ''
for each in parameters:
_in += each.get('in') \+ '\\n' # 参数传递位置
params += each.get('name') \+ '\\n' # 参数
\_in = \_in.strip('\\n')
\_in\_list = _in.split('\\n')
params = params.strip('\\n')
params_list = params.split('\\n')
del_list = params_list.copy() for i in range(len(\_in\_list)): if \_in\_list\[i\] == 'header':
params\_list.remove(del\_list\[i\]) # 只保存在body传的参数
test_list = params_list.copy()
params_dict = dict(zip(params\_list, test\_list)) # 把list转为dict
return params_dict def write_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'\])) except Exception as e:
self.log.info('Failure of interface use case to write to excel file! error:{}\\n'.format(e)) return
def diff_file(): """对比文件"""
global title_list for title in title_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 = read\_config.generate\_url.split(',') for i in url: # url_json = i + '/v2/api-docs?group=sign-api' # json swagger url地址
AnalysisJson(i).retrieve_data()
diff_file()
difference.py
import xlrd, time, difflib, sys, os from common.logger import Log, report_path
log = Log()
def diff_excel(ori_path, tar_path, sub_name): """比较excel文件""" success = 0 # 匹配一致数量
fail = 0 # 匹配不一致数量
origin_xls = {} # 存储源xls文件
target_xls = {} # 比对的xls文件
wb\_ori = xlrd.open\_workbook(ori_path) # 打开原始文件
wb\_tar = xlrd.open\_workbook(tar_path) # 打开目标文件
log.info(':【开始比对】...' \+ '\\n') # 写入开始时间
try:
sheet_ori = wb\_ori.sheet\_by\_name(sub\_name)
sheet_tar = wb\_tar.sheet\_by\_name(sub\_name) if sheet_ori.name == sheet_tar.name: # sheet表名
if sheet_ori.name == sub_name: # 先将数存入dictionary中dictionary(rows:list)
# 第一行存储表头
# 源表取一行数据与目标表全表进行比对如果表中存在主键可以用主键进行索引
# 数据从excel第3行开始
for rows in range(1, sheet_ori.nrows):
orign_list = sheet\_ori.row\_values(rows) # 源表i行数据
target\_list = sheet\_tar.row_values(rows) # 目标表i行数据
origin\_xls\[rows\] = orign\_list # 源表写入字典
target\_xls\[rows\] = target\_list # 目标表写入字典
if origin\_xls\[1\] == target\_xls\[1\]:
log.info('>>>>>>>>>>>>>>>>>>> 表头一致') for ori_num in origin_xls:
flag = 'false' # 判断是否一致标志
for tar_num in target_xls: if origin\_xls\[ori\_num\] == target\_xls\[tar\_num\]:
flag = 'true'
break # 如果匹配到结果退出循环
if flag == 'true': # 匹配上结果输出后台日志
success += 1
else: # 匹配不上将源表中行记录写入log
fail += 1 data = origin\_xls\[ori\_num\]
logstr = '【不一致】row<' \+ str(ori_num) + '>:' + str(data)
log.info(logstr)
logstr = '【比对完成】总记录数:{:d}条,一致:{:d}条,不一致:{:d}条'.format(ori_num, success, fail)
log.info(logstr) else:
errmsg = '【' \+ sub_name + '】子表名不一致' log.info(errmsg) except Exception as err:
log.info(str(err)) # 输出异常
def read_json(filename): try:
with open(filename, 'r') as fileHandle:
text = fileHandle.read().splitlines() return text except IOError as e:
log.error("Read file Error:" + e)
sys.exit() # 比较两个文件并输出到html文件中
def diff_json(filename1, filename2, name):
text1_lines = read_json(filename1)
text2_lines = read_json(filename2)
d = difflib.HtmlDiff() # context=True时只显示差异的上下文,默认显示5行,由numlines参数控制,context=False显示全文,差异部分颜色高亮,默认为显示全文
result = d.make\_file(text1\_lines, text2_lines, filename1, filename2, context=True) # 内容保存到result.html文件中
log.info('json数据比对结果写入html中.')
with open(os.path.join(report_path, '{}_diff.html'.format(name)), 'w') as result_file:
result_file.write(result) if \_\_name\_\_ == '\_\_main\_\_': for i in \['前台api', '后台api'\]:
diff_excel(r'G:\\Interface\_framework\_Beauty\\case\_generate\\data\_old\\demo_api.xlsx', 'G:\\Interface\_framework\_Beauty\\case\_generate\\data\_new\\demo_api.xlsx', '{}'.format(i))
diff_json('G:\\Interface\_framework\_Beauty\\case\_generate\\data\_old\\前台api_data.json', 'G:\\Interface\_framework\_Beauty\\case\_generate\\data\_new\\前台api_data.json', '')
执行成功后自动生成的 case,因为我使用的是ddt+requests+unittest框架,所以要把case集成到excel中。断言什么的需要手动输入...
data.json文件。使用的参数值需要手动输入...
生成的日志文件。从日志文件可以看出,case条数和对应的json参数数量是一致的.
可以根据生成的对比结果来修改接口数据
excel文件生成的log对比结果:
json文件生成的html对比结果:
各位大大有其他好的方法,欢迎留言一起讨论。
--------------------假装这里是分割线---------------------
writeExcel.py 文件
from openpyxl import load_workbook import openpyxl from openpyxl.styles import Font, colors class Write_excel(object): """修改excel数据"""
def \_\_init\_\_(self, filename):
self.filename = filename
self.wb = load_workbook(self.filename)
self.ws = self.wb.active # 激活sheet
def write(self, row\_n, col\_n, value): """写入数据,如(2,3,"hello"),第二行第三列写入数据"hello\\"""" ft = Font(color=colors.RED, size=12, bold=True) # 判断值为错误时添加字体样式
if value in \['fail', 'error'\] or col_n == 12:
self.ws.cell(row\_n, col\_n).font = ft if value == 'pass':
ft = Font(color=colors.GREEN)
self.ws.cell(row\_n, col\_n).font = ft
self.ws.cell(row\_n, col\_n).value = value
self.wb.save(self.filename) if \_\_name\_\_ == "\_\_main\_\_": # copy\_excel("demo\_api.xlsx", "test111.xlsx")
wt = Write_excel("test111.xlsx")
wt.write(4, 5, "HELLEOP")
wt.write(4, 6, "HELLEOP")
processingJson.py 文件
import json from common.logger import Log from jsonpath_rw import jsonpath, parse def write_data(res, json_path): """把处理后的参数写入json文件"""
if isinstance(res, dict):
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(res, f, indent=4)
Log().info('Interface Params Total:{} ,write to json file successfully!\\n'.format(len(res))) else:
Log().info('{} Params is not dict.\\n'.format(write_data.\_\_name\_\_))
logger.py 参考另一篇即可:[python logging模块 输出日志和过期清理
](https://www.cnblogs.com/changqing8023/p/9068273.html)此功能已集成到 EasyTest测试平台 中,欢迎大家体验~~~[
](https://www.cnblogs.com/changqing8023/p/9068273.html)
Original url: Access
Created at: 2019-11-08 16:20:51
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论