工作中常用xmind编写测试点,项目用例平台支持固定的模板导入,所以着手编写了一个 xmind转换 excel用例标准格式。
1.安装 三方库 xmindparser。
2.准备了两个文件,xmind_input.py 和 xmind_excel.py
xmind_excel.py,实现 xmind 转换为excel ;xmind_input.py 通过三方库PyQt5,实现了一个交互 。通过pyinstaller 三方库,打包为一个可执行工具,支持其他 window 上运行,打包中注意 需要完成 python 虚拟环境的搭建
PyQt5,支持直接进行xmind_input.ui文件,通过 pyuic 转换为 xmind_input.py。做好前端交互
xmind_excel.py 文件编写最耗时,需要把xmind识别的 结果,每个字段的循环写入对应excel 中。
代码如下:
def xmind_ex(xmind_name,case_type,case_suit):
all_markets = ["priority-1", "priority-2", "priority-3"]
# xm = xmind_to_dict(xmind_name)[0]['topic']#读取xmind 数据
# xm = xmind_to_dict("/Users/jay/Code/work/xmid_excel/1119.xmind")[0]['topic']
xm = xmind_to_dict(xmind_name)[0]['topic']
print(json.dumps(xm))
workbook = xlwt.Workbook(encoding='utf-8')#创建workbook对象
worksheet = workbook.add_sheet(xm["title"], cell_overwrite_ok=True)#创建工作表 并设置可以重写单元格内容
row0 =["所属模块",'相关需求','用例标题','前置条件','步骤','预期','优先级','用例类型','适用阶段']# 写成excel表格用例的要素
for iin range(len(row0)):
worksheet.write(0, i, row0[i])
x =0 # 写入数据的当前行数
z =0 # 用例的编号
test_case =""
test_suit =""
for iin range(len(xm["topics"])):
test_module = xm["topics"][i]
# test_module['title'] = "denglu"
markers_1 = test_module.get("makers")
if not markers_1and 'topics' in test_module:
for jin range(len(test_module["topics"])):
test_suit = test_module["topics"][j]
# test_suit['title'] = "ceshidian1"
markers_2 = test_suit.get("makers")
print(markers_2)
if not markers_2and 'topics' in test_suit:
for kin range(len(test_suit["topics"])):
test_case = test_suit["topics"][k]
z +=1
c1 =len(test_case["topics"])# 执行步骤有几个
markers_3 = test_case.get("makers")
print(markers_3)
if not markers_3and 'topics' in test_case:
for nin range(len(test_case["topics"])):
test_step = test_case["topics"][n]
x +=1
test_except = test_step["topics"][0]
x1 =len(test_step["topics"])
markers_4 = test_step.get("makers")
print(markers_4)
b4=markers_4[len(markers_4)-1]
print(b4)
print(b4[len(b4)-1])
if not markers_4and 'topics' in test_step:
pass
else:
for yin range(len(test_step["topics"])):
test_six = test_step["topics"][y]
test_x1 = test_six["topics"][0]
worksheet.write(x, 5, test_x1["title"])# 预期结果
print(markers_4[len(markers_4)-1])
# print(markers_4.split('-')[-1])
# worksheet.write(x, 6, markers_4.split('-')[-1]) # 优先级
worksheet.write(x, 6, b4[len(b4)-1])# 优先级
worksheet.write(x, 7, case_type)# 用例类型
worksheet.write(x, 8, case_suit)# 适用阶段
worksheet.write(x, 4, test_six["title"])# 预期结果
worksheet.write(x, 3, test_step["title"])# 执行步骤----
# worksheet.write_merge(x - c1 + 1, x, 0, 0, z) # 所属模块
worksheet.write_merge(x - c1 +1, x, 0, 0, test_module["title"])#所属模块
# worksheet.write_merge(x - c1 + 1, x, 1, 1, test_module["title"]) # 测试需求名称
worksheet.write_merge(x - c1 +1, x, 2, 2,
test_suit["title"] +"_" + test_case["title"])
else:
for nin range(len(test_case["topics"])):
b3 = markers_3[len(markers_3) -1]
test_step = test_case["topics"][n]
x +=1
test_except = test_step["topics"][0]
# worksheet.write(x, 6, markers_3.split('-')[-1]) # 优先级
worksheet.write(x, 6,b3[len(b3)-1])# 优先级
worksheet.write(x, 7, case_type)# 用例类型
worksheet.write(x, 8, case_suit)# 适用阶段
worksheet.write(x, 5, test_except["title"])# 预期结果
worksheet.write(x, 4, test_step["title"])# 执行步骤
# worksheet.write_merge(x - c1 + 1, x, 0, 0, z) # testcaseid
worksheet.write_merge(x - c1 +1, x, 0, 0, test_module["title"])# 所属模块
# worksheet.write_merge(x - c1 + 1, x, 1, 1, test_module["title"]) # 测试需求名称
worksheet.write_merge(x - c1 +1, x, 2, 2, test_suit["title"])
worksheet.write_merge(x - c1 +1, x, 3, 3, test_case["title"])
time = get_time_stamp()
newname = xm["title"] + time +".xls"
workbook.save(newname)# xls名称取xmind主题名称