一、项目背景
该工具提供将xmind文件,转换成测试案例excel文件。
在原1.0版本上,通过加上不同图标,识别前置条件、操作步骤、操作结果,并生成到EXCEL对应单元格中。
而思维导图中最后一列,仍旧固定设置为期望结果,将会把前面路径(除最后一列,以及未打图标的列)以“_”间隔为测试名称。
默认图标定义(可自行修改):
前置条件:优先级3
操作步骤:优先级4
期望结果:优先级5
二、实现效果如下
有xmind文件(思维导图.xmind):
生成的文件:
三、使用说明
命令行中执行python文件:
方式一(使用自行定义的输入(-i)&输出(-o)文件名(也可定义为其他excel格式如csv)):
| >python xmind2excel_V2.0.py -i [思维导图].xmind -o [测试案例].xls |
|---|
方式二(使用默认定义的输出文件名)(推荐):
| >python xmind2excel_V2.0.py -i [思维导图].xmind |
|---|
方式三(使用默认定义的输入(《思维导图.xmind》)&输出(《[输入文件名]_测试案例.xls》)文件名):
| >python freemind2excel_V2.0.py |
|---|
查看执行结果:
三、代码如下
xmind2excel_V2.0.py
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 ='priority-3'
# 设置操作步骤所在列、及其对应图标
steps_col_num =4
steps_markers ='priority-4'
# 设置期望结果所在列、及其对应图标
expect_col_num = 5
expect_markers ='priority-5'
# 设置用例类型所在列、用例类型
type_col_num = 8
testcase_type='功能测试'
# 设置用例“适用阶段”所在列、以及类型
stage_col_num = 9
testcase2_type='系统测试阶段'
# 用于设置插入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 or element['markers'].count(expect_markers) !=0:
value = element['title']
# print(row_num,':',value)
# 将思维导图中最后一列设置为期望结果,并写入excel
inser_excel(value, row_num, expect_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,'条用例。')



