问题:做测试的朋友们经常会用到xmind这个工具来梳理测试点或写测试用例,但是xmind8没有自带的统计测试用例,其他版本的xmind有些自带节点数量统计功能,但也也不会累计最终的数量,导致统计测试工作量比较困难。
解决方法:利用python开发小工具,实现同一份xmind文件中一个或多个sheet页的用例数量统计功能。
#!/usr/bin/env python
__author__ = 'zhongxintao'
import tkinter as tk
from tkinter import filedialog, messagebox
from xmindparser import xmind_to_dict
import xmind
class ParseXmind:
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
# 设置弹框标题、初始位置、默认大小
root.title(u'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))
# 设置2个Frame
self.frm1 = tk.Frame(root)
self.frm2 = tk.Frame(root)
# 设置弹框布局
self.frm1.grid(row=1, padx='20', pady='20')
self.frm2.grid(row=2, padx='30', pady='30')
self.but\_upload = tk.Button(self.frm1, text=u'上传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=u"开始统计", command=self.new\_lines, bg='#dfdfdf')
self.but2.grid(row=0, columnspan=6, pady='10')
self.label\_file = tk.Label(self.frm2, text=u"文件名", relief='groove', borderwidth='2', width=25,
bg='#FFD0A2')
self.label\_file.grid(row=1, column=0)
self.label\_case = tk.Label(self.frm2, text=u"用例数", relief='groove', borderwidth='2', width=10,
bg='#FFD0A2').grid(row=1, column=1)
self.label\_pass = tk.Label(self.frm2, text=u"成功", relief='groove', borderwidth='2', width=10,
bg='#FFD0A2').grid(row=1, column=2)
self.label\_fail = tk.Label(self.frm2, text=u"失败", relief='groove', borderwidth='2', width=10,
bg='#FFD0A2').grid(row=1, column=3)
self.label\_block = tk.Label(self.frm2, text=u"阻塞", 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中的用例数"""
for i in range(len(li)):
if li\[i\].\_\_contains\_\_('topics'):
# 带topics标签表示有子标题,递归执行方法
self.count\_case(li\[i\]\['topics'\])
# 不带topics表示无子标题,此级别即是用例
else:
# 有标记成功或失败时会有makers标签
if li\[i\].\_\_contains\_\_('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):
"""用例统计表新增一行"""
# sheets是一个list,可包含多sheet页
sheets = xmind\_to\_dict(filename) # 调用此方法,将xmind转成字典
for sheet in sheets:
print(sheet)
# 字典的值sheet\['topic'\]\['topics'\]是一个list
my\_list = sheet\['topic'\]\['topics'\]
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):
"""用例统计表新增多行"""
# 从text中获取所有行
lines = self.text.get(1.0, tk.END)
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中"""
select\_files = tk.filedialog.askopenfilenames(title=u"可选择1个或多个文件")
for file in select\_files:
self.text.insert(tk.END, file + '\\n')
self.text.update()
if __name__ == '__main__':
r = tk.Tk()
ParseXmind(r)
r.mainloop()
1、xmind文件中使用下列图标进行分类标识:
标记表示p0级别case:数字1
标记表示执行通过case:绿色√
标记表示执行失败case:红色×
标记表示执行阻塞case:橙色!
2、执行代码
3、在弹框内【上传xmind文件】按钮
4、在弹框内【开始统计】按钮
手机扫一扫
移动阅读更方便
你可能感兴趣的文章