前言
每個公司都有一個維護測試case的系統,有自研的也有買的,比如QC, 禪道等等,QA往往習慣使用xmind等思維導圖工具來編寫測試用例。
因為思路清晰,編寫方便,那么這就有一個問題,大多數公司要求所有的case都要導入到系統統一維護,然而系統對xmind的支持并不友好,或者根本不支持,就我們目前的情況來說,系統支持導入xmind文件導入,但是導入后所有的用例都是亂的,而且沒有測試步驟,沒有預期結果等等問題,因此針對這一痛點,便誕生了今天的小工具,雖然這個工具只能解決我的問題,但是里面有大家可以學習參考的地方,希望對你有幫助,那么我的目的就達到了。
一、工具源碼
"""
import tkinter as tk
from tkinter import filedialog, messagebox
import xmind
import os
from xmindparser import xmind_to_dict
class ParseXmind(object):
def __init__(self, root):
self.count = 0
self.case_fail = 0
self.case_success = 0
self.case_block = 0
self.case_priority = 0
# total匯總用
self.total_cases = 0
self.total_success = 0
self.total_fail = 0
self.total_block = 0
self.toal_case_priority = 0
# GUI
root.title('Xmind用例個數統計及文件解析')
width = 760
height = 600
xscreen = root.winfo_screenwidth()
yscreen = root.winfo_screenheight()
xmiddle = (xscreen - width) / 2
ymiddle = (yscreen - height) / 2
root.geometry('%dx%d+%d+%d' % (width, height, xmiddle, ymiddle)) # 窗口默認大小
# 3個frame
self.frm1 = tk.Frame(root)
self.frm2 = tk.Frame(root)
self.frm3 = tk.Frame(root)
# 布局
self.frm1.grid(row = 1, padx = '20', pady = '20')
self.frm2.grid(row = 2, padx = '30', pady = '30')
self.frm3.grid(row = 0, padx = '40', pady = '40')
self.lable = tk.Label(self.frm3, text = "Xmind文件完整路徑")
self.lable.grid(row = 0, column = 0, pady = '5')
self.file_path = tk.Entry(self.frm3, bd = 2)
self.file_path.grid(row = 0, column = 1, pady = '5')
def get_full_file_path_text():
"""
獲取xmind文件完整路徑
:return:
"""
full_xmind_path = self.file_path.get() # 獲取文本框內容
# 簡單對輸入內容做一個校驗
if full_xmind_path == "" or "xmind" not in full_xmind_path:
messagebox.showinfo(title = "warning", message = "xmind文件路徑錯誤!")
try:
self.create_new_xmind(full_xmind_path)
except FileNotFoundError:
pass
else:
xmind_file = full_xmind_path[:-6].split("/")[-1] # xmind文件名,不帶后綴
path_list = full_xmind_path[:-6].split("/") # xmind文件用/分割后的一個列表
path_list.pop(0)
path_list.pop(-1)
path_full = "/" + "/".join(path_list) # xmind文件的目錄
new_xmind_file = "{}/{}_new.xmind".format(path_full, xmind_file) # 新的xmind文件完整路徑
messagebox.showinfo(title = "success", message = "已生成新的xmind文件:{}".format(new_xmind_file))
# 頁面的一些空間的布局
self.button = tk.Button(self.frm3, text = "提交", width = 10, command = get_full_file_path_text, bg = '#dfdfdf')
self.button.grid(row = 0, column = 2, pady = '5')
self.but_upload = tk.Button(self.frm1, text = '上傳xmind文件', command = self.upload_files, bg = '#dfdfdf')
self.but_upload.grid(row = 0, column = 0, pady = '10')
self.text = tk.Text(self.frm1, width = 55, height = 10, bg = '#f0f0f0')
self.text.grid(row = 1, column = 0)
self.but2 = tk.Button(self.frm2, text = "開始統計", command = self.new_lines, bg = '#dfdfdf')
self.but2.grid(row = 0, columnspan = 6, pady = '10')
self.label_file = tk.Label(self.frm2, text = "文件名", relief = 'groove', borderwidth = '2', width = 25,
bg = '#FFD0A2')
self.label_file.grid(row = 1, column = 0)
self.label_case = tk.Label(self.frm2, text = "用例數", relief = 'groove', borderwidth = '2', width = 10,
bg = '#FFD0A2').grid(row = 1, column = 1)
self.label_pass = tk.Label(self.frm2, text = "成功", relief = 'groove', borderwidth = '2', width = 10,
bg = '#FFD0A2').grid(row = 1, column = 2)
self.label_fail = tk.Label(self.frm2, text = "失敗", relief = 'groove', borderwidth = '2', width = 10,
bg = '#FFD0A2').grid(row = 1, column = 3)
self.label_block = tk.Label(self.frm2, text = "阻塞", relief = 'groove', borderwidth = '2', width = 10,
bg = '#FFD0A2').grid(row = 1, column = 4)
self.label_case_priority = tk.Label(self.frm2, text = "p0case", relief = 'groove', borderwidth = '2',
width = 10, bg = '#FFD0A2').grid(row = 1, column = 5)
def count_case(self, li):
"""
統計xmind中的用例數
:param li:
:return:
"""
for i in range(len(li)):
if li[i].__contains__('topics'): # 帶topics標簽意味著有子標題,遞歸執行方法
self.count_case(li[i]['topics'])
else: # 不帶topics意味著無子標題,此級別既是用例
if li[i].__contains__('makers'): # 有標記成功或失敗時會有makers標簽
for mark in li[i]['makers']:
if mark == "symbol-right": # 成功
self.case_success += 1
elif mark == "symbol-wrong": # 失敗
self.case_fail += 1
elif mark == "symbol-attention": # 阻塞
self.case_block += 1
elif mark == "priority-1": # 優先級
self.case_priority += 1
self.count += 1 # 用例總數
def new_line(self, filename, row_number):
"""
用例統計表新增一行
:param filename:
:param row_number:
:return:
"""
# 調用Python/ target=_blank class=infotextkey>Python中xmind_to_dict方法,將xmind轉成字典
sheets = xmind_to_dict(filename) # sheets是一個list,可包含多sheet頁;
for sheet in sheets:
print(sheet)
my_list = sheet['topic']['topics'] # 字典的值sheet['topic']['topics']是一個list
# print(my_list)
self.count_case(my_list)
# 插入一行統計數據
lastname = filename.split('/')
self.label_file = tk.Label(self.frm2, text = lastname[-1], relief = 'groove', borderwidth = '2', width = 25)
self.label_file.grid(row = row_number, column = 0)
self.label_case = tk.Label(self.frm2, text = self.count, relief = 'groove', borderwidth = '2', width = 10)
self.label_case.grid(row = row_number, column = 1)
self.label_pass = tk.Label(self.frm2, text = self.case_success, relief = 'groove', borderwidth = '2',
width = 10)
self.label_pass.grid(row = row_number, column = 2)
self.label_fail = tk.Label(self.frm2, text = self.case_fail, relief = 'groove', borderwidth = '2', width = 10)
self.label_fail.grid(row = row_number, column = 3)
self.label_block = tk.Label(self.frm2, text = self.case_block, relief = 'groove', borderwidth = '2', width = 10)
self.label_block.grid(row = row_number, column = 4)
self.label_case_priority = tk.Label(self.frm2, text = self.case_priority, relief = 'groove', borderwidth = '2',
width = 10)
self.label_case_priority.grid(row = row_number, column = 5)
self.total_cases += self.count
self.total_success += self.case_success
self.total_fail += self.case_fail
self.total_block += self.case_block
self.toal_case_priority += self.case_priority
def new_lines(self):
"""
用例統計表新增多行
:return:
"""
lines = self.text.get(1.0, tk.END) # 從text中獲取所有行
row_number = 2
for line in lines.splitlines(): # 分隔成每行
if line == '':
break
print(line)
self.new_line(line, row_number)
row_number += 1
# total匯總行
self.label_file = tk.Label(self.frm2, text = 'total', relief = 'groove', borderwidth = '2', width = 25)
self.label_file.grid(row = row_number, column = 0)
self.label_case = tk.Label(self.frm2, text = self.total_cases, relief = 'groove', borderwidth = '2', width = 10)
self.label_case.grid(row = row_number, column = 1)
self.label_pass = tk.Label(self.frm2, text = self.total_success, relief = 'groove', borderwidth = '2',
width = 10)
self.label_pass.grid(row = row_number, column = 2)
self.label_fail = tk.Label(self.frm2, text = self.total_fail, relief = 'groove', borderwidth = '2', width = 10)
self.label_fail.grid(row = row_number, column = 3)
self.label_block = tk.Label(self.frm2, text = self.total_block, relief = 'groove', borderwidth = '2',
width = 10)
self.label_block.grid(row = row_number, column = 4)
self.label_case_priority = tk.Label(self.frm2, text = self.toal_case_priority, relief = 'groove',
borderwidth = '2',
width = 10)
self.label_case_priority.grid(row = row_number, column = 5)
def upload_files(self):
"""
上傳多個文件,并插入text中
:return:
"""
select_files = tk.filedialog.askopenfilenames(title = "可選擇1個或多個文件")
for file in select_files:
self.text.insert(tk.END, file + 'n')
self.text.update()
@staticmethod
def parse_xmind(path):
"""
xmind變為一個字典
:param path:
:return:
"""
dic_xmind = xmind_to_dict(path)
xmind_result = dic_xmind[0]
return xmind_result
def create_new_xmind(self, path):
"""
用原xmind內容新建一個xmind文件
:param path:
:return:
"""
xmind_result = self.parse_xmind(path)
new_xmind_result = self.dict_result(xmind_result)
xmind_file = path[:-6].split("/")[-1]
path_list = path[:-6].split("/")
path_list.pop(0)
path_list.pop(-1)
path_full = "/" + "/".join(path_list)
new_xmind_file = "{}/{}_new.xmind".format(path_full, xmind_file)
print(new_xmind_file)
if os.path.exists(new_xmind_file):
os.remove(new_xmind_file)
xmind_wb = xmind.load(new_xmind_file)
new_xmind_sheet = xmind_wb.getSheets()[0]
new_xmind_sheet.setTitle(new_xmind_result["sheet_topic_name"])
root_topic = new_xmind_sheet.getRootTopic()
root_topic.setTitle(new_xmind_result["sheet_topic_name"])
for k, v in new_xmind_result["data"].items():
topic = root_topic.addSubTopic()
topic.setTitle(k)
for value in v:
new_topic = topic.addSubTopic()
new_topic.setTitle(value)
xmind.save(xmind_wb)
def dict_result(self, xmind_result):
"""
使用原xmind數據構造new_xmind_result
:param xmind_result:
:return:
"""
sheet_name = xmind_result['title']
sheet_topic_name = xmind_result['topic']['title'] # 中心主題名稱
new_xmind_result = {
'sheet_name': sheet_name,
'sheet_topic_name': sheet_topic_name,
'data': {}
}
title_list = []
for i in xmind_result['topic']['topics']:
title_temp = i['title']
title_list.Append(title_temp)
result_list = self.chain_data(i)
new_xmind_result['data'][title_temp] = result_list
return new_xmind_result
@staticmethod
def chain_data(data):
"""
原xmind的所有topic連接成一個topic
:param data:
:return:
"""
new_xmind_result = []
def calculate(s, prefix):
prefix += s['title'] + '->'
for t in s.get('topics', []):
s1 = calculate(t, prefix)
if s1 is not None:
new_xmind_result.append(s1.strip('->'))
if not s.get('topics', []):
return prefix
calculate(data, '')
return new_xmind_result
if __name__ == '__main__':
r = tk.Tk()
ParseXmind(r)
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
3.開始統計
上傳xmind文件(支持上傳多個xmind文件)->點擊[開始統計]->展示成功,失敗,阻塞,p0case統計數
六、工具效果
1.xmind格式化及導入aone
原xmind文件測試case
導入aone效果
導入后的測試case名稱混亂,很難按照這種格式來執行測試,如果不經過修改無法作為業務測試的沉淀,更不用說其他人用這樣的測試case來進行業務測試了
格式化后的xmind測試case
工具把所有的xmind 的節點進行了一個拼接,這樣看起來case的步驟也清晰很多
導入aone
aone上展示的用例名稱,可以顯示完整的case的執行步驟,不需要再在case內部補充測試步驟,也不需要做多余的case維護
2.用例統計
總結
今天的文章就到這里了喲喜歡的小伙伴可以點贊收藏評論加關注喲。