文章目录
- python脚本小工具
- 1、json文件的对比
- 2、excel文件的对比
- 2.1、附上代码及注释
python脚本小工具
没事的时候,或者叫忙里偷闲,写写python,提升一下代码能力;
前段时间写了对比文件的python工具代码,需要手工来对比数据;
略显鸡肋,这是我们开发同学工作未做足,接口文档不标识变更;
1、json文件的对比
- 第一版就实现了,并且生成html报告,略显麻烦;
思路:因为生成的json文件都在某个目录下,所以先对比md5,再判断内容,生成对应的html报告
2、excel文件的对比
- 第一版的实现很不友好,需要人工对比,筛选出用例做出标记
思路:将json格式数据的文件解析生成excel用例,然后再逐条对比excel的数据,并写入text文本
- 第二版的实现也有问题,只是做了一点优化
思路:不需要将excel的行的所有列进行对比,只抽取其中的某几列,如:接口url及参数params
- 第三版的实现是对上面的代码又一次优化
思路:继续抽取其中的编号列,然后统一写入一个新的excel文件中,比写入文本更加直观
- 第四版的实现,应该可以算上终极版本
思路:我不想把有变更的用例重写文件,而是直接在原文件中做标记(添加背景色),可能会用到数据驱动或者unittest单元测试框架
2.1、附上代码及注释
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @desc : 对比接口excel
import xlrd
import xlwt
import time
import config
from common.HandleLogging import log
class DiffExcelFile():
'''对比excel文件的工具类'''
def __init__(self,wb_name="Excel_Workbook.xlsx",sheet_name="Sheet1"):
#创建工作簿对象
self.workbook = xlwt.Workbook(encoding = 'ascii')
#初始化实例属性
self.wb_name=wb_name
self.sheet_name=sheet_name
#创建表单对象
self.worksheet = self.workbook.add_sheet(self.sheet_name)
def write_excel(self,row,col,content,style='pattern: pattern solid, fore_colour yellow; font: bold on'):
'''写入文件对象,传入行和列及写入内容,style样式默认'''
# 设置表单样式
style=xlwt.easyxf(style)
# 写入表单
self.worksheet.write(row, col, label = content, style=style) # Apply the Style to the Cell
# 保存表单
self.save_excel()
def save_excel(self):
'''保存工作对象'''
self.workbook.save(self.wb_name)
#往日志文件中追加内容函数#个人感觉这个很鸡肋,实际以日志输出就可以
def write_file(filename,content):
'''
写入文件,将对比不同的测试用例写入日志
'''
if not isinstance(content,str):
content=str(content)
with open(filename,'a',encoding='utf-8') as file: #以追加方式打开日志文件
time_now= time.strftime("%Y-%m-%d", time.localtime()) #系统时间格式化
file.write(time_now+':变更的接口及参数==>'+content+'\n') #写入内容
def read_excel(file_path,sheet_name="Sheet1"):#
'''读取excel表格,返回一个list所有列数据'''
datas=[] #储存xlsx文件的所有数据
xlsx_file={} #存储源xls文件
wb=xlrd.open_workbook(file_path) #打开目标文件
# sheet_num = len(wb.sheets()) #获取xlsx表单数量
sheet_name_list=wb.sheet_names() #获取xlsx表单名字
if sheet_name in sheet_name_list:
sheet_name=wb.sheet_by_name(sheet_name)
for rows in range(0,sheet_name.nrows):
orign_list=sheet_name.row_values(rows) #源表i行数据
xlsx_file[rows]=orign_list #源表写入字典
else:
log.info("{}子表名不存在{}文件中!".format(sheet_name,file_path))
for row in range(1,len(xlsx_file)):
data=dict(zip(xlsx_file[0],xlsx_file[row]))
datas.append(data)
return datas
# 初始化对比写入excel文件
xw=DiffExcelFile(wb_name=config.datas_path+"test124.xlsx")
def diff_excel(src_file,des_file,check="caseid,url,params"):
'''对比文件的数据某个字段的值,默认sheet_name=Sheet1'''
fail=0 #记录变更的数据
res1=read_excel(src_file)
res2=read_excel(des_file)
# 提取的关键列的title
lis1=check.split(",")
index=lis1[0]
check1=lis1[1]
check2=lis1[2]
data=[]
for i in range(len(res2)):
data.append([res2[i][check1],res2[i][check2]])
datas=[]
for r1 in range(len(res1)):
case=[res1[r1][check1],res1[r1][check2]]
if case not in data:
log.info("新增/变更数据:{}".format(case))
fail+=1
case_id=str(res1[r1][index])
content="".join([case_id,str(case)])
datas.append(content)
write_file(config.logs_path+"diff_data.log",content)
for i in range(len(datas)):
xw.write_excel(i+1, 0, datas[i])
if __name__ == '__main__':
# write_excel(1,2,content="123",wb_name=config.datas_path+"test124.xlsx")
res=diff_excel(config.datas_path+"test123.xlsx", config.datas_path+"test321.xlsx")
# read_excel(config.datas_path+"test123.xlsx",config.datas_path+"test321.xlsx","Sheet1")
日拱一卒无有尽,功不唐捐终入海!