很多测试人员使用的xmind编写测试用例,条理清晰且直观,但是面临一个使用后归档的问题,如果直接放到svn或者git上还算简单,有些是要上传到禅道等系统进行用例管理,按模板一条一条的拷贝任务量就大了,于是就参考文档自己编写了一个导入禅道的工具
1、规范用例模板xmin,因为解析的时候是按模板来的,如果模板变化太大可能需要修改代码
导入禅道的csv模板(这几个字段导入就可以了,删减了一些字段)
2、运行环境
python版本:python3.x
lib包:xmindparser,xlwt,pandas 可pip直接安装
3、代码详情
from xmindparser import xmind_to_dict
import os, sys
import xlwt
import pandas as pd
class Xmind2Csv:
def toCsv(path):
xm = xmind_to_dict(path)[0]['topic'] #读取xmind数据
# print(json.dumps(xm, indent=2, ensure_ascii=False))
workbook = xlwt.Workbook(encoding='GBK') #创建workbook
worksheet = workbook.add_sheet(xm['title'], cell_overwrite_ok=True) #创建sheet
row0 = ['所属模块', '用例标题', '前置条件', '步骤', '预期', '关键词', '优先级','用例类型', '适用阶段']
for i in range(len(row0)):
worksheet.write(0, i, row0[i])
x = 0 #写入数据的当前函数
for i in range(len(xm['topics'])):
test_module = xm['topics'][i] #获取测试模块
# print(test_module['title'])
for j in range(len(test_module['topics'])):
test_suit = test_module['topics'][j] #获取功能或用例集
# print(test_suit)
for k in range(len(test_suit['topics'])):
test_case = test_suit['topics'][k] #获取用例标题
# print(test_case)
# print(test_case['topics'])
if test_case.get('topics'):
for n in range(len(test_case['topics'])):
test_step = test_case['topics'][n] #获取步骤
# print(test_step)
x += 1
if(test_module['title'] == "机器人"):
worksheet.write(x, 0, "/机器人(#84)")
elif(test_module['title'] == "控制器"):
worksheet.write(x, 0, "/控制器(#85)")
elif (test_module['title'] == "设计器"):
worksheet.write(x, 0, "/设计器(#86)")
else:
worksheet.write(x, 0, "")
worksheet.write(x, 1, test_case['title'])
worksheet.write(x, 2, "")
worksheet.write(x, 3, test_step['title'])
if test_step.get('topics'):
worksheet.write(x, 4, test_step['topics'][0]['title'])
else:
worksheet.write(x, 4, "")
worksheet.write(x, 5, "")
worksheet.write(x, 6, "3")
worksheet.write(x, 7, "功能测试")
worksheet.write(x, 8, "功能测试阶段")
else:
x += 1
if (test_module['title'] == "机器人"):
worksheet.write(x, 0, "/机器人(#84)")
elif (test_module['title'] == "控制器"):
worksheet.write(x, 0, "/控制器(#85)")
elif (test_module['title'] == "设计器"):
worksheet.write(x, 0, "/设计器(#86)")
else:
worksheet.write(x, 0, "")
worksheet.write(x, 1, test_case['title'])
worksheet.write(x, 2, "")
worksheet.write(x, 3, "")
worksheet.write(x, 4, "")
worksheet.write(x, 5, "")
worksheet.write(x, 6, "3")
worksheet.write(x, 7, "功能测试")
worksheet.write(x, 8, "功能测试阶段")
workbook.save(xm["title"] + ".xls")
data_xls = pd.read_excel(xm["title"] + ".xls", index_col=0)
data_xls.to_csv(xm["title"] + ".csv", encoding='GBK')
os.remove(xm["title"] + ".xls")
if __name__ == "__main__":
Xmind2Csv.toCsv(sys.argv[1])
# Xmind2Csv.toCsv("0515版本.xmind")
解释:先把xmind内容解析成xml,然后根据json取值存入到xls中,禅道支持的是csv格式,所以就转了一下格式保存为csv格式并删除xls
4、在xmind存放文件夹路径上面输入cmd或者打开cmd到用例文件夹下,执行如下命令,如:
python xmind2excel.py 0515版本.xmind
5、数据导入到禅道
选择GBK格式和生成的csv文件