安装库
pip install openpyxl xmindparser
代码实现
import os.path
import traceback
from openpyxl.styles import Font, Alignment
from openpyxl.workbook import Workbook
from xmindparser import xmind_to_dict
class Xmind2ExcelCases:
"""
xmind测试需求分析转换测试用例
xmind分支书写约定:项目名称-模块1-子模块1...-用例名称-操作步骤-预期结果
"""
def __init__(self, xmind_path):
self.xmind_path = xmind_path
self.save_dir = os.path.dirname(xmind_path)
def read_xmind(self):
"""读取xmind文件,返回中心标题和分支列表"""
case_data_dict = xmind_to_dict(self.xmind_path)[0]["topic"]
title = case_data_dict["title"]
data_list = case_data_dict["topics"]
return title, data_list
def xmind2cases(self, data_list, case_list=[], strcase=''):
"""
根据传入的分支列表数据,递归解析出每一个完整分支
返回使用"&&&"拼接的分支列表,如['模块1&&&子模块1&&&用例名称&&&操作步骤&&&预期结果',...]
"""
for branch_one in data_list:
strcase_one = strcase + branch_one['title']
if 'topics' not in branch_one:
strcase_one = strcase_one
case_list.append(strcase_one)
continue
branch = branch_one['topics']
self.xmind2cases(branch, case_list, strcase=strcase_one + "&&&")
return case_list
def conv_cases(self, strcase_list, case_type='功能测试', case_grade='中'):
"""
单用例结构:[用例目录,用例名称,用例步骤,预期结果,用例类型,用例等级]
返回转换后的用例列表,如[[用例1],[用例2]...]
"""
total_case = []
for one_case in strcase_list:
try:
one_case_list = []
case_info = one_case.split('&&&')
one_case_list.append(case_info[-3])
one_case_list.append(case_info[-2])
one_case_list.append(case_info[-1])
one_case_list.insert(0, f"{case_type}-" + "-".join(case_info[:-3]))
one_case_list.append(case_type)
one_case_list.append(case_grade)
total_case.append(one_case_list)
except Exception:
print(traceback.format_exc())
continue
return total_case
def write_excel_cases(self, total_case, save_path):
"""把解析的数据写入Excel中"""
wb = Workbook()
ws = wb.active
ws['A1'] = '用例目录'
ws['B1'] = '用例名称'
ws['C1'] = '用例步骤'
ws['D1'] = '预期结果'
ws['E1'] = '用例类型'
ws['F1'] = '用例等级'
i = 1
for case in total_case:
ws['A{}'.format(i + 1)] = case[0]
ws['B{}'.format(i + 1)] = case[1]
ws['C{}'.format(i + 1)] = case[2]
ws['D{}'.format(i + 1)] = case[3]
ws['E{}'.format(i + 1)] = case[4]
ws['F{}'.format(i + 1)] = case[5]
i += 1
column_list = ['A', 'B', 'C', 'D', 'E', 'F']
cell_list = ['A1', 'B1', 'C1', 'D1', 'E1', 'F1']
font = Font(name='宋体', bold=True)
for cel in cell_list:
cell = ws[cel]
cell.font = font
alignment = Alignment(horizontal='center', wrapText=True)
for col_name in column_list:
col_list = ws[col_name]
for col in col_list:
col.alignment = alignment
ws.column_dimensions['A'].width = 20
ws.column_dimensions['B'].width = 40
ws.column_dimensions['C'].width = 40
ws.column_dimensions['D'].width = 40
ws.column_dimensions['E'].width = 10
ws.column_dimensions['F'].width = 10
for x in range(2, ws.max_row + 1):
ws.row_dimensions[x].height = 40
wb.save(save_path)
def run(self):
title, data_list = self.read_xmind()
case_list = self.xmind2cases(data_list, [])
total_case = self.conv_cases(case_list)
save_path = os.path.join(self.save_dir, f"{title}_测试用例.xlsx")
self.write_excel_cases(total_case, save_path)
print(f"用例转换完成: {save_path}")
if __name__ == '__main__':
Xmind2ExcelCases(xmind_path="测试需求分析模板.xmind").run()