韶关市网站建设_网站建设公司_Angular_seo优化
2026/1/3 6:29:33 网站建设 项目流程

背景

利用python生成测试用例,直接生成的测试用例,如果不是测试平台合适的xmind思维导图格式,还需要手动去复制粘贴比较麻烦;思路是给ai大模型投喂需求文档,然后吐出表格形式的用例case再生成对应格式xmind文件导入测试平台;

 

1,读取excel文件生成xmind文件

import xmind import openpyxl import os def add_prefix_if_needed(text, prefix): """如果 text 没有以指定前缀开头,则自动添加""" if not text: return text if not text.strip().startswith(prefix): return f"{prefix}{text}" return text def read_excel_and_generate_xmind(excel_path, xmind_path): # 确保输出目录存在 os.makedirs(os.path.dirname(xmind_path) if os.path.dirname(xmind_path) else '.', exist_ok=True) # 如果目标 XMind 文件已存在,先删除它(实现覆盖) if os.path.exists(xmind_path): os.remove(xmind_path) print(f"🗑️ 已删除旧文件: {xmind_path}") # 创建全新的 XMind 工作簿(此时文件不存在,会新建) xmind_wb = xmind.load(xmind_path) # 自动创建空文件 sheet_xmind = xmind_wb.getPrimarySheet() root_topic = sheet_xmind.getRootTopic() root_topic.setTitle("测试用例") # 加载 Excel workbook = openpyxl.load_workbook(excel_path) sheet = workbook.active module_topics = {} for row in sheet.iter_rows(min_row=2, values_only=True): module, title, pre_cond, steps, expected = row if not module or not title: continue # 增加前缀 formatted_title = add_prefix_if_needed(title,"case:\n") formatted_pre_cond = add_prefix_if_needed(pre_cond,"Preq:\n") if module not in module_topics: module_topic = root_topic.addSubTopic() module_topic.setTitle(module) module_topics[module] = module_topic parent_module = module_topics[module] case_topic = parent_module.addSubTopic() case_topic.setTitle(formatted_title) pc_topic = case_topic.addSubTopic() pc_topic.setTitle(formatted_pre_cond) step_topic = case_topic.addSubTopic() step_topic.setTitle(steps) result_topic = step_topic.addSubTopic() result_topic.setTitle(expected) # 保存(覆盖写入) xmind.save(xmi

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询