一、项目背景
该工具提供将xmind文件,转换成测试案例excel文件。
在原1.0版本上,通过加上不同图标,识别前置条件、操作步骤、操作结果,并生成到EXCEL对应单元格中。
而思维导图中最后一列,仍旧固定设置为期望结果,将会把前面路径(除最后一列,以及未打图标的列)以“_”间隔为测试名称。
前置条件:蓝色警号(图标) 操作步骤:绿色钢笔(图标) 期望结果:在期望结果打优先级标签,可以在EXCEL写入对应优先级。如打“优先级1”,则生成的EXCEL中写入对应优先级“1”。
二、实现效果如下
有xmind文件(思维导图.xmind):
![](https://img-blog.csdnimg.cn/788827d99537443cacc71cc4f58e6b2c.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY2F0NWNhdDY=,size_20,color_FFFFFF,t_70,g_se,x_16)
生成的文件:
![](https://img-blog.csdnimg.cn/8502f3d478f6466c998b21d7049a8751.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY2F0NWNhdDY=,size_20,color_FFFFFF,t_70,g_se,x_16)
三、使用说明
命令行中执行python文件:
方式一(使用自行定义的输入(-i)&输出(-o)文件名(也可定义为其他excel格式如csv)):
>python xmind2excel.py -i [思维导图].xmind -o [测试案例].xls
方式二(使用默认定义的输出文件名)(推荐):
>python xmind2excel.py -i [思维导图].xmind
方式三(使用默认定义的输入(《思维导图.xmind》)&输出(《[输入文件名]_测试案例.xls》)文件名):
>python freemind2excel.py
查看执行结果:
![](https://img-blog.csdnimg.cn/14b15ce0d5a946929a922013d8978648.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY2F0NWNhdDY=,size_20,color_FFFFFF,t_70,g_se,x_16)
三、代码如下
import xmind
import sys
import xlwt
import argparse
import os
import time
# 设置从第n+1行开始插入excel
row_num = 1
# 设置用例名称所在列(0为第一列)
name_col_num = 2
# 设置前置条件所在列、及其对应图标
condition_col_num =3
condition_markers ='symbol-info'
# 设置操作步骤所在列、及其对应图标
steps_col_num =4
steps_markers ='c_symbol_pen'
# 设置期望结果所在列、及其对应图标
expect_col_num = 5
# 设置用例类型所在列、用例类型
type_col_num = 8
testcase_type='功能测试'
# 设置用例“适用阶段”所在列、以及类型
stage_col_num = 9
testcase2_type='系统测试阶段'
# 设置用户优先级所在列
priority_col_num = 7
# 用于设置插入excel的标题
def set_excel_header():
n=0
header=['所属模块','用例编号','用例标题','前置条件','步骤','预期','关键词','优先级','用例类型','适用阶段','是否自动化','自动化平台','自动化用例路径']
for i in header:
ws.write(0,n,i)
n+=1
# 该方法用于插入excel
def inser_excel(text,row,col):
ws.write(row,col,text)
# 该方法用于生成用例名称,格式为模块1_模块2_模块3_模块4_……,
def per_round(element,str):
global row_num
if element['markers'].count(condition_markers) !=0:
# 判断当前是否为前置条件
# print("这是前置条件")
condition_desc = element['title']
inser_excel(condition_desc,row_num,condition_col_num)
elif element['markers'].count(steps_markers) !=0:
# 判断当前是否为操作步骤
# print("这是操作步骤")
steps_desc = element['title']
inser_excel(steps_desc,row_num,steps_col_num)
else:
str += '_' + element['title']
for child in element['topics']:
per_fun(child,str)
value=child['title']
# print('--------',value)
# print(child)
if len(child)==7:
inser_excel(str, row_num, name_col_num)
inser_excel(testcase_type, row_num, type_col_num)
inser_excel(testcase2_type,row_num, stage_col_num)
row_num+=1
def per_fun(element,str):
global row_num
if len(element) == 7 :
value = element['title']
# print(row_num,':',value)
# 将思维导图中最后一列设置为期望结果,并写入excel
inser_excel(value, row_num, expect_col_num)
# 开始设置优先级
if len(element['markers'])!=0:
priority = element['markers'][0]
if priority == 'priority-1':
inser_excel(1, row_num, priority_col_num)
elif priority == 'priority-2':
inser_excel(2, row_num, priority_col_num)
elif priority == 'priority-3':
inser_excel(3, row_num, priority_col_num)
elif priority == 'priority-4':
inser_excel(4, row_num, priority_col_num)
else:
inser_excel(4, row_num, priority_col_num)
else:
inser_excel(3, row_num, priority_col_num)
else:
# 将思维导图中除最后一列外,以"_"间隔组成用例名称
per_round(element, str)
# 重新设置生成文件名
def set_file_name(input_filename,output_filename):
if output_filename=='测试案例.xls':
str=input_filename.replace('.xmind','_')+output_filename
path2 = os.getcwd() + '\\' + str
if os.path.exists(path2):
time1 = time.strftime("%Y%m%d%H%M%S", time.localtime())
str = input_filename.replace('.xmind', '_') + '测试案例'+time1+'.xls'
return str
else:
path3=os.getcwd()+'\\'+output_filename
if os.path.exists(path3) :
print('检测到当前路径下已有存在文件:', output_filename, '\n是否继续覆盖并生成文件?Y:继续')
isg = input()
if isg == 'Y' or isg == 'y':
os.remove(path3)
else:
print(output_filename, '文件生成操作已取消。')
sys.exit()
return output_filename
parser = argparse.ArgumentParser()
parser.add_argument('-i','--input',type=str,dest='inputfile', default='思维导图.xmind', help='Default inputfile is 思维导图.xmind')
parser.add_argument('-o', '--output-file', type=str, dest='outputfile', default='测试案例.xls', help='Default outputfile is 测试案例.xls')
args=parser.parse_args()
if args.inputfile is None:
parser.print_help()
exit()
path3=os.getcwd()+'\\'+args.inputfile
if not os.path.exists(path3) :
print('\n文件\"'+path3+'\"不存在,请确认后再次操作,谢谢!')
exit()
file_name=set_file_name(args.inputfile,args.outputfile)
print('开始将',args.inputfile,'文件转换为excel。。。')
workbook = xmind.load(args.inputfile)
sheet = workbook.getPrimarySheet()
root_topic = sheet.getRootTopic()
root = root_topic.getData()
# print(root)
str = '#'
sheet = xlwt.Workbook()
ws = sheet.add_sheet('禅道导入模板', cell_overwrite_ok=True)
set_excel_header()
per_round(root,str)
sheet.save(file_name)
print('导出完成,已生成文件:',file_name,',总计',row_num-1,'条用例。')
|