如何利用python將Xmind用例轉為Excel用例
1、Xmind用例編寫規(guī)范

- 1:需求大模塊
- 2:大模塊中的小模塊(需要根據(jù)需求來看需要多少層)
- 3:用例等級和用例名稱
- 用例等級(轉換成Excel文件后,1為High, 2 為 Middle, 3為Low)
- 轉換成excel時,用例的名稱為(框出來的1-2-3組合而成),意味著在標等級及之前的節(jié)點會組合成用例名稱
- 4:步驟
- 5:期望結果
- 6:預置條件,轉換成excel時相同層級下的用例會為同一個預置條件
2、轉換代碼
需要安裝python3環(huán)境
需要安裝 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 = ["測試用例編號", "用例標題", "預置條件", "執(zhí)行方式", "優(yōu)先級", "測試步驟", "預期結果", "所屬項目"]
? ? # 生成第一行中固定表頭內(nèi)容
? ? 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, "手動") ?# 執(zhí)行方式
? ? ? ? ? ? ? ? ? ? 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("請檢查編寫的用例是否符合規(guī)范:", lists[j])
? ? ? ? ? ? # 遍歷結束lists,給增量索引賦值,跳出for j循環(huán),開始for h循環(huán)
? ? ? ? ? ? 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運行\(zhòng)n3.檢查xmind文件中不能有亂碼或無法識別的字符(xmind自帶表情字符除外)\n4.檢查是否將已生成的excel文件未關閉')3、使用
運行XmindExcel.py文件,輸入文件目錄運行即可。生成的Excel文件會在Xmind文件的同路徑下,文件名稱與Xmind文件名稱一致

到此這篇關于如何利用python將Xmind用例轉為Excel用例的文章就介紹到這了,更多相關python Excel用例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)socket客戶端和服務端簡單示例
這篇文章主要介紹了python實現(xiàn)socket客戶端和服務端簡單示例,需要的朋友可以參考下2014-02-02
詳解Python logging調(diào)用Logger.info方法的處理過程
這篇文章主要介紹了詳解Python logging調(diào)用Logger.info方法的處理過程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
PyQt5使用mimeData實現(xiàn)拖拽事件教程示例解析上
這篇文章主要為大家介紹了PyQt中如何使用mimeData實現(xiàn)拖拽事件的示例解析過程,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進步2021-10-10
pandas 把數(shù)據(jù)寫入txt文件每行固定寫入一定數(shù)量的值方法
今天小編就為大家分享一篇pandas 把數(shù)據(jù)寫入txt文件每行固定寫入一定數(shù)量的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

