经常遇到问题:
- excel编写测试用例,评审时看的比较密集,使评审结果未能达到预期?
- xmind编写测试用例,逻辑清晰,可进行折叠展示,但公司要求excel?
- 如果有工具xmind格式转换excel就好?
那么,以下工具帮你解决上述问题,但需满足下面格式要求:
代码实现: 所需库:openpyxl、wxpython、xmindparser xmind转化excel 实现可视化Gui
xmind转化excel 读取xmind数据
from xmindparser import xmind_to_dict
def readXmind( FileName):
XmindContent = xmind_to_dict(FileName)[0]['topic']
print(XmindContent)
XmindFile = r'E:\桌面\用例模板.xmind'
readXmind(XmindFile)
用例层级数据处理
rowNum = 1
caseDict = {}
FeaturesNUM = numberLen(XmindContent)
for i in range(FeaturesNUM):
TestFeatures = numberLen(XmindContent['topics'][i])
if TestFeatures != 0:
for j in range(TestFeatures):
TestPoints = numberLen(
XmindContent['topics'][i]['topics'][j])
if TestPoints != 0:
for k in range(TestPoints):
TestTypes = numberLen(XmindContent['topics'][i]['topics'][j]['topics'][k])
if TestTypes != 0:
for m in range(TestTypes):
TestItems = numberLen(XmindContent['topics'][i]['topics'][j]['topics'][k]['topics'][m])
if TestItems != 0:
for case in range(TestItems):
caseDict['用例模块'] = XmindContent['topics'][i]['title']
caseDict['用例标题'] = XmindContent['topics'][i]['topics'][j]['title'] + '/' + XmindContent['topics'][i]['topics'][j]['topics'][k]['title'] + '/' + XmindContent['topics'][i]['topics'][j]['topics'][k]['topics'][m]['title']
caseDict['用例步骤'] = XmindContent['topics'][i]['topics'][j]['topics'][k]['topics'][m]['topics'][case]['title']
try:
caseDict['预期结果'] = XmindContent['topics'][i]['topics'][j]['topics'][k]['topics'][m]['topics'][case]['topics'][0]['title']
except Exception as e:
print(e)
rowNum = rowNum + 1
writeExcel(self.rowNum, self.caseDict)
处理用例数
def numberLen(value):
try:
return len(value['topics'])
except KeyError:
return 0
excel文件处理
import openpyxl
class xmind_to_csv:
def __init__(self,excelName):
self.excelName=excelName
self.filename = self.excelName + '.xlsx'
self.workbook = openpyxl.Workbook()
try:
self.workbook.save(self.filename)
self.workbook = openpyxl.load_workbook(self.filename)
self.worksheet = self.workbook.create_sheet(title=self.excelName, index=0)
del self.workbook['Sheet']
except Exception:
raise "文件已打开,请关闭重试"
写入excel表格处理
def numberLen(value):
try:
return len(value['topics'])
except KeyError:
return 0
可视化GUI处理(wx库) 页面实现
class MyFrame1(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"Xmind转换Excel格式工具", pos=wx.DefaultPosition, size=wx.Size(345, 180),
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
bSizer1 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"Xmind路径:", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText1.Wrap(-1)
self.m_staticText1.SetFont(
wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, wx.EmptyString))
self.m_staticText1.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT))
bSizer1.Add(self.m_staticText1, 0, wx.ALL | wx.EXPAND, 5)
self.m_filePicker4 = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"选择文件",
u"xmind文件(*.xmind)|*.xmind", wx.DefaultPosition,
wx.DefaultSize, wx.FLP_DEFAULT_STYLE | wx.FLP_FILE_MUST_EXIST,
wx.DefaultValidator, u"浏览")
bSizer1.Add(self.m_filePicker4, 0, wx.ALL | wx.EXPAND, 5)
gSizer1 = wx.GridSizer(1, 2, 0, 0)
gSizer1.SetMinSize(wx.Size(20, 20))
self.m_button2 = wx.Button(self, wx.ID_ANY, u"确定", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button2.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
gSizer1.Add(self.m_button2, 0, wx.ALL, 5)
self.m_button1 = wx.Button(self, wx.ID_ANY, u"取消", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button1.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
gSizer1.Add(self.m_button1, 0, wx.ALL | wx.ALIGN_RIGHT, 5)
bSizer1.Add(gSizer1, 0, wx.EXPAND, 5)
bSizer7 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText2 = wx.StaticText(self, wx.ID_ANY, u"工具人:Menghui Ding",
wx.DefaultPosition, wx.Size(-1, 26), 0)
self.m_staticText2.Wrap(-1)
self.m_staticText2.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT))
bSizer7.Add(self.m_staticText2, 0, 0, 5)
self.m_panel1 = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
bSizer7.Add(self.m_panel1, 1, wx.ALL | wx.EXPAND, 5)
bSizer1.Add(bSizer7, 1, wx.EXPAND, 5)
self.SetSizer(bSizer1)
self.Layout()
self.m_statusBar1 = self.CreateStatusBar(1, wx.STB_SIZEGRIP, wx.ID_ANY)
self.m_button2.Bind(wx.EVT_BUTTON, self.m_button2OnButtonClick)
self.m_button1.Bind(wx.EVT_BUTTON, self.m_button1OnButtonClick)
事件逻辑
def __del__(self):
pass
def m_button1OnButtonClick(self, event):
wx.Exit()
def m_button2OnButtonClick(self, event):
XmindFile_path = self.m_filePicker4.GetPath()
if "xmind" in XmindFile_path == 0:
wx.MessageBox("路径错误", "提示",wx.ICON_ERROR)
else:
try:
xmindFile = XmindFile_path.split("\\")[-1].split('.')[-2]
xmind_to_csv(xmindFile).readXmind(XmindFile_path)
wx.MessageBox("转换完成", "提示",wx.ICON_INFORMATION)
wx.Exit()
except Exception as erro:
wx.MessageBox("请选择正确路径", "提示",wx.ICON_ERROR)
event.Skip()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame1(None)
frame.Show()
app.MainLoop()
|