XMind测试需求分析转Excel测试用例

文章介绍了如何使用Python库(如openpyxl和xmindparser)将Xmind测试需求分析转换为Excel测试用例,包括读取Xmind文件、解析分支结构、生成单个用例结构,最后写入Excel工作簿。
摘要由CSDN通过智能技术生成

安装库

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值