提效工具-python解析xmind檔案及xmind用例統計
現狀
每個公司都有一個維護測試case的系統,有自研的也有買的,比如QC, 禪道等等,QA往往習慣使用xmind等思維導圖工具來編寫測試用例,因為思路清晰,編寫方便,那麼這就有一個問題,大多公司要求所有的case都要匯入到系統統一維護,然而系統對xmind的支援並不友好,或者根本不支援,就我們目前的情況來說,系統支援匯入xmind檔案匯入,但是匯入後所有的用例都是亂的,而且沒有測試步驟,沒有預期結果等等問題,因此針對這一痛點,便誕生了今天的小工具,雖然這個工具只能解決我的問題,但是裡面有大家可以學習參考的地方,希望對你有幫助,那麼我的目的就達到了
工具原始碼
1 """ 2 ------------------------------------ 3 @Time : 2020/9/24 3:21 PM 4 @Auth : linux超 5 @File : handleXmind.py 6 @IDE : PyCharm 7 @Motto: ABC(Always Be Coding) 8 ------------------------------------ 9 """ 10 import tkinter as tk 11 from tkinter import filedialog, messagebox 12 import xmind 13 import os 14 from xmindparser import xmind_to_dict 15 16 17 class ParseXmind(object): 18 19 def __init__(self, root): 20 self.count = 0 21 self.case_fail = 0 22 self.case_success = 0 23 self.case_block = 0 24 self.case_priority = 0 25 # total彙總用 26 self.total_cases = 0 27 self.total_success = 0 28 self.total_fail = 0 29 self.total_block = 0 30 self.toal_case_priority = 0 31 # GUI 32 root.title('Xmind用例個數統計及檔案解析') 33 width = 760 34 height = 600 35 xscreen = root.winfo_screenwidth() 36 yscreen = root.winfo_screenheight() 37 xmiddle = (xscreen - width) / 2 38 ymiddle = (yscreen - height) / 2 39 root.geometry('%dx%d+%d+%d' % (width, height, xmiddle, ymiddle)) # 視窗預設大小 40 41 # 3個frame 42 self.frm1 = tk.Frame(root) 43 self.frm2 = tk.Frame(root) 44 self.frm3 = tk.Frame(root) 45 # 佈局 46 self.frm1.grid(row = 1, padx = '20', pady = '20') 47 self.frm2.grid(row = 2, padx = '30', pady = '30') 48 self.frm3.grid(row = 0, padx = '40', pady = '40') 49 50 self.lable = tk.Label(self.frm3, text = "Xmind檔案完整路徑") 51 self.lable.grid(row = 0, column = 0, pady = '5') 52 self.file_path = tk.Entry(self.frm3, bd = 2) 53 self.file_path.grid(row = 0, column = 1, pady = '5') 54 55 def get_full_file_path_text(): 56 """ 57 獲取xmind檔案完整路徑 58 :return: 59 """ 60 full_xmind_path = self.file_path.get() # 獲取文字框內容 61 # 簡單對輸入內容做一個校驗 62 if full_xmind_path == "" or "xmind" not in full_xmind_path: 63 messagebox.showinfo(title = "warning", message = "xmind檔案路徑錯誤!") 64 try: 65 self.create_new_xmind(full_xmind_path) 66 except FileNotFoundError: 67 pass 68 else: 69 xmind_file = full_xmind_path[:-6].split("/")[-1] # xmind檔名,不帶字尾 70 path_list = full_xmind_path[:-6].split("/") # xmind檔案用/分割後的一個列表 71 path_list.pop(0) 72 path_list.pop(-1) 73 path_full = "/" + "/".join(path_list) # xmind檔案的目錄 74 new_xmind_file = "{}/{}_new.xmind".format(path_full, xmind_file) # 新的xmind檔案完整路徑 75 messagebox.showinfo(title = "success", message = "已生成新的xmind檔案:{}".format(new_xmind_file)) 76 77 # 頁面的一些空間的佈局 78 self.button = tk.Button(self.frm3, text = "提交", width = 10, command = get_full_file_path_text, bg = '#dfdfdf') 79 self.button.grid(row = 0, column = 2, pady = '5') 80 81 self.but_upload = tk.Button(self.frm1, text = '上傳xmind檔案', command = self.upload_files, bg = '#dfdfdf') 82 self.but_upload.grid(row = 0, column = 0, pady = '10') 83 self.text = tk.Text(self.frm1, width = 55, height = 10, bg = '#f0f0f0') 84 self.text.grid(row = 1, column = 0) 85 self.but2 = tk.Button(self.frm2, text = "開始統計", command = self.new_lines, bg = '#dfdfdf') 86 self.but2.grid(row = 0, columnspan = 6, pady = '10') 87 self.label_file = tk.Label(self.frm2, text = "檔名", relief = 'groove', borderwidth = '2', width = 25, 88 bg = '#FFD0A2') 89 self.label_file.grid(row = 1, column = 0) 90 self.label_case = tk.Label(self.frm2, text = "用例數", relief = 'groove', borderwidth = '2', width = 10, 91 bg = '#FFD0A2').grid(row = 1, column = 1) 92 93 self.label_pass = tk.Label(self.frm2, text = "成功", relief = 'groove', borderwidth = '2', width = 10, 94 bg = '#FFD0A2').grid(row = 1, column = 2) 95 self.label_fail = tk.Label(self.frm2, text = "失敗", relief = 'groove', borderwidth = '2', width = 10, 96 bg = '#FFD0A2').grid(row = 1, column = 3) 97 self.label_block = tk.Label(self.frm2, text = "阻塞", relief = 'groove', borderwidth = '2', width = 10, 98 bg = '#FFD0A2').grid(row = 1, column = 4) 99 self.label_case_priority = tk.Label(self.frm2, text = "p0case", relief = 'groove', borderwidth = '2', 100 width = 10, bg = '#FFD0A2').grid(row = 1, column = 5) 101 102 def count_case(self, li): 103 """ 104 統計xmind中的用例數 105 :param li: 106 :return: 107 """ 108 for i in range(len(li)): 109 if li[i].__contains__('topics'): # 帶topics標籤意味著有子標題,遞迴執行方法 110 self.count_case(li[i]['topics']) 111 else: # 不帶topics意味著無子標題,此級別既是用例 112 if li[i].__contains__('makers'): # 有標記成功或失敗時會有makers標籤 113 for mark in li[i]['makers']: 114 if mark == "symbol-right": # 成功 115 self.case_success += 1 116 elif mark == "symbol-wrong": # 失敗 117 self.case_fail += 1 118 elif mark == "symbol-attention": # 阻塞 119 self.case_block += 1 120 elif mark == "priority-1": # 優先順序 121 self.case_priority += 1 122 self.count += 1 # 用例總數 123 124 def new_line(self, filename, row_number): 125 """ 126 用例統計表新增一行 127 :param filename: 128 :param row_number: 129 :return: 130 """ 131 132 # 呼叫python中xmind_to_dict方法,將xmind轉成字典 133 sheets = xmind_to_dict(filename) # sheets是一個list,可包含多sheet頁; 134 for sheet in sheets: 135 print(sheet) 136 my_list = sheet['topic']['topics'] # 字典的值sheet['topic']['topics']是一個list 137 # print(my_list) 138 self.count_case(my_list) 139 140 # 插入一行統計資料 141 lastname = filename.split('/') 142 self.label_file = tk.Label(self.frm2, text = lastname[-1], relief = 'groove', borderwidth = '2', width = 25) 143 self.label_file.grid(row = row_number, column = 0) 144 145 self.label_case = tk.Label(self.frm2, text = self.count, relief = 'groove', borderwidth = '2', width = 10) 146 self.label_case.grid(row = row_number, column = 1) 147 self.label_pass = tk.Label(self.frm2, text = self.case_success, relief = 'groove', borderwidth = '2', 148 width = 10) 149 self.label_pass.grid(row = row_number, column = 2) 150 self.label_fail = tk.Label(self.frm2, text = self.case_fail, relief = 'groove', borderwidth = '2', width = 10) 151 self.label_fail.grid(row = row_number, column = 3) 152 self.label_block = tk.Label(self.frm2, text = self.case_block, relief = 'groove', borderwidth = '2', width = 10) 153 self.label_block.grid(row = row_number, column = 4) 154 self.label_case_priority = tk.Label(self.frm2, text = self.case_priority, relief = 'groove', borderwidth = '2', 155 width = 10) 156 self.label_case_priority.grid(row = row_number, column = 5) 157 self.total_cases += self.count 158 self.total_success += self.case_success 159 self.total_fail += self.case_fail 160 self.total_block += self.case_block 161 self.toal_case_priority += self.case_priority 162 163 def new_lines(self): 164 """ 165 用例統計表新增多行 166 :return: 167 """ 168 169 lines = self.text.get(1.0, tk.END) # 從text中獲取所有行 170 row_number = 2 171 for line in lines.splitlines(): # 分隔成每行 172 if line == '': 173 break 174 print(line) 175 self.new_line(line, row_number) 176 row_number += 1 177 178 # total彙總行 179 self.label_file = tk.Label(self.frm2, text = 'total', relief = 'groove', borderwidth = '2', width = 25) 180 self.label_file.grid(row = row_number, column = 0) 181 self.label_case = tk.Label(self.frm2, text = self.total_cases, relief = 'groove', borderwidth = '2', width = 10) 182 self.label_case.grid(row = row_number, column = 1) 183 184 self.label_pass = tk.Label(self.frm2, text = self.total_success, relief = 'groove', borderwidth = '2', 185 width = 10) 186 self.label_pass.grid(row = row_number, column = 2) 187 self.label_fail = tk.Label(self.frm2, text = self.total_fail, relief = 'groove', borderwidth = '2', width = 10) 188 self.label_fail.grid(row = row_number, column = 3) 189 self.label_block = tk.Label(self.frm2, text = self.total_block, relief = 'groove', borderwidth = '2', 190 width = 10) 191 self.label_block.grid(row = row_number, column = 4) 192 193 self.label_case_priority = tk.Label(self.frm2, text = self.toal_case_priority, relief = 'groove', 194 borderwidth = '2', 195 width = 10) 196 self.label_case_priority.grid(row = row_number, column = 5) 197 198 def upload_files(self): 199 """ 200 上傳多個檔案,並插入text中 201 :return: 202 """ 203 select_files = tk.filedialog.askopenfilenames(title = "可選擇1個或多個檔案") 204 for file in select_files: 205 self.text.insert(tk.END, file + '\n') 206 self.text.update() 207 208 @staticmethod 209 def parse_xmind(path): 210 """ 211 xmind變為一個字典 212 :param path: 213 :return: 214 """ 215 dic_xmind = xmind_to_dict(path) 216 xmind_result = dic_xmind[0] 217 return xmind_result 218 219 def create_new_xmind(self, path): 220 """ 221 用原xmind內容新建一個xmind檔案 222 :param path: 223 :return: 224 """ 225 xmind_result = self.parse_xmind(path) 226 new_xmind_result = self.dict_result(xmind_result) 227 xmind_file = path[:-6].split("/")[-1] 228 path_list = path[:-6].split("/") 229 path_list.pop(0) 230 path_list.pop(-1) 231 path_full = "/" + "/".join(path_list) 232 new_xmind_file = "{}/{}_new.xmind".format(path_full, xmind_file) 233 print(new_xmind_file) 234 if os.path.exists(new_xmind_file): 235 os.remove(new_xmind_file) 236 xmind_wb = xmind.load(new_xmind_file) 237 new_xmind_sheet = xmind_wb.getSheets()[0] 238 new_xmind_sheet.setTitle(new_xmind_result["sheet_topic_name"]) 239 root_topic = new_xmind_sheet.getRootTopic() 240 root_topic.setTitle(new_xmind_result["sheet_topic_name"]) 241 for k, v in new_xmind_result["data"].items(): 242 topic = root_topic.addSubTopic() 243 topic.setTitle(k) 244 for value in v: 245 new_topic = topic.addSubTopic() 246 new_topic.setTitle(value) 247 xmind.save(xmind_wb) 248 249 def dict_result(self, xmind_result): 250 """ 251 使用原xmind資料構造new_xmind_result 252 :param xmind_result: 253 :return: 254 """ 255 sheet_name = xmind_result['title'] 256 sheet_topic_name = xmind_result['topic']['title'] # 中心主題名稱 257 new_xmind_result = { 258 'sheet_name': sheet_name, 259 'sheet_topic_name': sheet_topic_name, 260 'data': {} 261 } 262 title_list = [] 263 for i in xmind_result['topic']['topics']: 264 title_temp = i['title'] 265 title_list.append(title_temp) 266 result_list = self.chain_data(i) 267 new_xmind_result['data'][title_temp] = result_list 268 return new_xmind_result 269 270 @staticmethod 271 def chain_data(data): 272 """ 273 原xmind的所有topic連線成一個topic 274 :param data: 275 :return: 276 """ 277 new_xmind_result = [] 278 279 def calculate(s, prefix): 280 prefix += s['title'] + '->' 281 for t in s.get('topics', []): 282 s1 = calculate(t, prefix) 283 if s1 is not None: 284 new_xmind_result.append(s1.strip('->')) 285 if not s.get('topics', []): 286 return prefix 287 288 calculate(data, '') 289 return new_xmind_result 290 291 292 if __name__ == '__main__': 293 r = tk.Tk() 294 ParseXmind(r) 295 r.mainloop()
工具打包
目前我打的是mac系統的包,打完包可以放到任意一個mac電腦上使用(windows版本打包可以自行百度,很簡單)
1.pip安裝py2app
2.待打包檔案目錄下執行命令py2applet --make-setup baba.py
3.執行命令python setup.py py2app
4.生成的app在dist資料夾內,雙擊即可執行
工具介面
工具功能
1.格式化xmind測試case
2.統計xmind測試case通過,失敗,阻塞,p0級case數量
工具使用
1.xmind格式化
輸入框需要輸入xmind檔案的完整路徑->點選[提交]->生成新的xmind檔案(新檔案與原始xmind在一個目錄下)->新的xmind匯入aone(我們目前的用例管理工具)即可
2.用例統計
xmind用例標記規則
標記表示p0級別case
標記表示p0級別case
標記表示執行通過case
標記表示執行失敗case
標記表示執行阻塞case
開始統計
上傳xmind檔案(支援上傳多個xmind檔案)->點選[開始統計]->展示成功,失敗,阻塞,p0case統計數
工具效果
1.xmind格式化及匯入aone
原xmind檔案測試case
匯入aone效果
匯入後的測試case名稱混亂,很難按照這種格式來執行測試,如果不經過修改無法作為業務測試的沉澱,更不用說其他人用這樣的測試case來進行業務測試了
格式化後的xmind測試case
工具把所有的xmind 的節點進行了一個拼接,這樣看起來case的步驟也清晰很多
匯入aone
aone上展示的用例名稱,可以顯示完整的case的執行步驟,不需要再在case內部補充測試步驟,也不需要做多餘的case維護
2.用例統計
&n