日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

如何利用python將Xmind用例轉為Excel用例_python

作者:sinat_18866031 ? 更新時間: 2022-08-01 編程語言

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文件名稱一致

原文鏈接:https://blog.csdn.net/sinat_18866031/article/details/125086436

欄目分類
最近更新