如何利用python將Xmind用例轉為Excel用例
1、Xmind用例編寫規范
- 1:需求大模塊
- 2:大模塊中的小模塊(需要根據需求來看需要多少層)
- 3:用例等級和用例名稱
- 用例等級(轉換成Excel文件後,1為High, 2 為 Middle, 3為Low)
- 轉換成excel時,用例的名稱為(框出來的1-2-3組合而成),意味著在標等級及之前的節點會組合成用例名稱
- 4:步驟
- 5:期望結果
- 6:預置條件,轉換成excel時相同層級下的用例會為同一個預置條件
2、轉換代碼
需要安裝python3環境
需要安裝 xlwt、xmindparser 這兩個第三方包
XmindExcel.py 文件代碼
# coding=utf-8 import time import xlwt from past.builtins import raw_input from xmindparser import xmind_to_dict def resolvePath(dict, lists, title): # title去除首尾空格 title = title.strip() # 如果title是空字符串,則直接獲取value if len(title) == 0: concatTitle = dict['title'].strip() elif "makers" in dict.keys(): if "priority-" in str(dict["makers"]): concatTitle = title + '\t' + dict['title'].strip() + "\t" + str(dict["makers"]) else: concatTitle = title + '\t' + dict['title'].strip() else: concatTitle = title + '\t' + dict['title'].strip() if dict.__contains__('topics') == False: lists.append(concatTitle) else: for d in dict['topics']: resolvePath(d, lists, concatTitle def xmind_cat(list, excelname, groupname): f = xlwt.Workbook() sheet = f.add_sheet(groupname, cell_overwrite_ok=True) row0 = ["測試用例編號", "用例標題", "預置條件", "執行方式", "優先級", "測試步驟", "預期結果", "所屬項目"] # 生成第一行中固定表頭內容 for i in range(0, len(row0)): sheet.write(0, i, row0[i]) # 增量索引 index = 0 # case級別 case_leve_index = "" # 前置條件 case_pre_condition = [] pre_num = 0 for h in range(0, len(list)): # print("list:",list) lists = [] resolvePath(list[h], lists, '') for j in range(0, len(lists)): # 將xmind轉成excel lists[j] = lists[j].split('\t') try: # print(index) if "【預置條件】" in lists[j][-1] or "【前置條件】" in lists[j][-1]: case_pre_condition.append(lists[j]) pre_num += 1 else: case_leve = "" for n in range(len(lists[j])): if 'priority-' in str(lists[j][n]): case_leve_index = n-1 if "priority-1" in str(lists[j][n]): case_leve = "High" elif "priority-2" in str(lists[j][n]): case_leve = "Middle" elif "priority-3" in str(lists[j][n]): case_leve = "Low" lists[j].pop(n) break case_name = "-".join(lists[j][:case_leve_index+1]) sheet.write(j + index + 1 - pre_num, 1, case_name) # 標題 if len(lists[j][case_leve_index:-1]) < 2: sheet.write(j + index + 1 - pre_num, 6, lists[j][case_leve_index + 1]) # 期望結果 else: sheet.write(j + index + 1- pre_num, 5, lists[j][case_leve_index + 1]) # 步驟 sheet.write(j + index + 1- pre_num, 6, lists[j][case_leve_index + 2]) # 期望結果 sheet.write(j + index + 1- pre_num, 3, "手動") # 執行方式 sheet.write(j + index + 1 - pre_num, 4, case_leve) # 預置條件 if len(case_pre_condition) > 0: for pre_list in case_pre_condition: if set(pre_list[:-1]) < set(lists[j]): sheet.write(j + index + 1 - pre_num, 2, pre_list[-1]) except: print("請檢查編寫的用例是否符合規范:", lists[j]) # 遍歷結束lists,給增量索引賦值,跳出for j循環,開始for h循環 if j == len(lists) - 1: index += len(lists) f.save(excelname) def maintest(filename, excelname): out = xmind_to_dict(filename) groupname = out[0]['topic']['title'] xmind_cat(out[0]['topic']['topics'], excelname, groupname) if __name__ == '__main__': try: path = raw_input("請輸入Xmind用例文件路徑,可將文件拖拽到此處:") filename = path excelname = path.rstrip('xmind') + 'xls' maintest(filename, excelname) print('SUCCESS!\n生成用例成功,用例目錄:%s' % excelname) except: print('請確認後重試:\n1.用例文件路徑中不能有空格換行符\n2.請使用python3運行\n3.檢查xmind文件中不能有亂碼或無法識別的字符(xmind自帶表情字符除外)\n4.檢查是否將已生成的excel文件未關閉')
3、使用
運行XmindExcel.py文件,輸入文件目錄運行即可。生成的Excel文件會在Xmind文件的同路徑下,文件名稱與Xmind文件名稱一致
到此這篇關於如何利用python將Xmind用例轉為Excel用例的文章就介紹到這瞭,更多相關python Excel用例內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- python 使用openpyxl讀取excel數據
- python 按照sheet合並多個Excel的示例代碼(多個sheet)
- python讀寫修改Excel之xlrd&xlwt&xlutils
- 深入淺析Python數據分析的過程記錄
- 淺談Python xlwings 讀取Excel文件的正確姿勢