- ** 转换工具界面:**
1.选择xmind所在路径
2.输入用例操作人
3.提交等待xls生成
- ** Xmind编写用例规范:**
用例目录:填写测试项目名称
用例名称:填写用例所属模块名称
前置条件:填写用例前置条件
用例步骤:填写用例步骤
预期结果:填写预期结果
用例类型:默认写死“功能测试”---故无需操作
(tapd上导入模板仅支持功能测试/性能测试/安全性测试/其它4种)
用例状态:默认写死’正常’---故无需操作
(tapd上导入模板仅支持正常/待更新/已废弃3种--本打算在预期结果处-插入图标-任务进度)
用例等级:前置条件处-插入图标-任务优先级,只能选1-高,2-中,3-低
pyinstaller -F xmind_excel.py -p xmind_xls.py -w
- ** 转换生成的excel测试案例**
--后续可直接导入tapd的标准格式
上代码:
xmind_excel.py
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showinfo
from my_xmind_csv_002 import xmind_to_csv
import re
# 定义MainUI类表示应用/窗口,继承Frame类
class MainUI(tk.Frame):
# Application构造函数,master为窗口的父控件
def __init__(self, master=None):
# 初始化Application的Frame部分
tk.Frame.__init__(self, master)
# 显示窗口,并使用grid布局
self.grid()
self.path = tk.StringVar()
self.operator = tk.StringVar()
# 创建控件
self.createWidgets()
def selectPath(self):
'''选择要转换成excel的xmind地址'''
self.path_ = askopenfilename()
self.path.set(self.path_)
# 创建控件
def createWidgets(self):
'''生成gui界面'''
# 创建一个标签,输出要显示的内容
self.firstLabel = tk.Label(self, text="目标路径")
# 设定使用grid布局
self.firstLabel.grid(row = 0, column = 0)
self.firstEntry = tk.Entry(self,textvariable = self.path)
self.firstEntry.grid(row=0, column=1)
# 创建一个按钮,用来触发answer方法
self.firstLabel1 = tk.Label(self, text="用例操作人")
self.firstLabel1.grid(row = 2, column = 0)
self.firstEntry1 = tk.Entry(self,textvariable = self.operator)
self.firstEntry1.grid(row=2, column=1)
print(self.firstEntry1.get())
self.clickButton = tk.Button(self, text="路径选择", command=self.selectPath)
# 设定使用grid布局
self.clickButton.grid(row = 0, column = 2)
self.clickButton = tk.Button(self, text="提交",command=self.getvalue)
# 设定使用grid布局
self.clickButton.grid(row=4, column=1)
def getvalue(self):
'''执行转换excel函数'''
xmindPath = self.path.get()
operator = self.operator.get()
self.regvalue = '.*\.xmind$'
self.xmind_reg = re.match(self.regvalue,xmindPath )
if self.xmind_reg:
# xmind转换成xls
self.xmind_to_xls = xmind_to_csv()
self.xmind_to_xls.readXmind(xmindPath,operator)
else:
showinfo(title='提示',message='请选择正确的xmind文件')
if __name__ == '__main__':
# 创建一个MainUI对象
app = MainUI()
# 设置窗口标题
app.master.title('Xmind转excel ')
# 设置窗体大小
app.master.geometry('290x90')
app.master.resizable(False, False)
# 主循环开始
app.mainloop()
xmind_xls.py
from xmindparser import xmind_to_dict
import os,xlwt
from tkinter.messagebox import showinfo
import tkinter.messagebox
class xmind_to_csv():
def __init__(self):
self.workbook = xlwt.Workbook(encoding='utf-8')
self.worksheet = self.workbook.add_sheet('sheet1',cell_overwrite_ok=True)
def numberLen(self,value,errornum=None):
try:
return len(value['topics'])
except KeyError:
if errornum==2:
tkinter.messagebox.askokcancel('提示', '用例有前置条件 "{0}",没有测试步骤和预期结果喔! 请确认是否如此!'.format(value['title']))
print('案例 "{0}",没有测试步骤和预期结果喔! 请确认是否如此!'.format(value['title']))
if errornum == 3:
tkinter.messagebox.showinfo(title='提示', message='用例有前置条件 "{0}",没有预期结果喔! 请填写后重新执行!'.format(value['title']))
print('案例 "{0}",没有预期结果喔! 请填写后重新执行!'.format(value['title']))
return 0
def xmind_title(self,value):
"""获取xmind标题内容"""
return value['title']
def writeExcel(self,row,case,excelName):
sort=0
row0 = ["用例目录", '用例名称', '前置条件', '用例步骤', '预期结果',"用例类型", '用例状态', '用例等级', '创建人']
#生成第一行
for i in range(0,len(row0)):
self.worksheet.write(0,i,row0[i])
for key,value in case.items():
self.worksheet.write(row, sort, value)
sort=sort+1
self.workbook.save(excelName+'.xls')
def readXmind(self,FileName,operator='ditto'):
self.rowNum = 0 #计算测试用例的条数
self.caseDict={}
self.XmindContent = xmind_to_dict(FileName)[0]['topic'] # xmind内容
print(self.XmindContent)
self.XmindTitle=self.xmind_title(self.XmindContent)
print(self.XmindTitle)
TestSuitMunFlag=self.numberLen(self.XmindContent,0)
for TestSuitMun in range(TestSuitMunFlag):
TestCaseMunFlag=self.numberLen(self.XmindContent['topics'][TestSuitMun],1)
print('TestCaseMunFlag+'+str(TestCaseMunFlag))
for TestCaseMun in range(TestCaseMunFlag):
TestStepMunFlag=self.numberLen(self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun],2)
print('TestStepMunFlag+'+str(TestStepMunFlag))
#执行步骤跟预期结果为空的时候
if TestStepMunFlag==0:
self.caseDict['myTestCase']= self.XmindContent['title']
self.caseDict['TestCase'] = self.XmindContent['topics'][TestSuitMun]['title']
self.caseDict['Testprecondition'] = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['title']
self.caseDict['TestStep']=''
self.caseDict['TestResult'] = ''
self.caseDict['Testbelong']=''
self.caseDict['Teststatus']='正常'#self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['makers'][0]
Testpriority1 = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['makers'][0]
if Testpriority1 == 'priority-1':
Testpriority= '高'
elif Testpriority1 == 'priority-2':
Testpriority= '中'
else:
Testpriority= '低'
self.caseDict['Testpriority']=Testpriority
self.caseDict['Testoperator']=operator
# print(self.caseDict['Testpriority'])
# self.caseDict['myTestCase'] = self.caseDict['TestSuit'] + '/' + self.caseDict['TestCase']
# print(self.rowNum,self.caseDict['TestSuit'] + '/' + self.caseDict['TestCase'])
self.rowNum = self.rowNum + 1
print("*************")
self.writeExcel(self.rowNum,self.caseDict,self.XmindTitle)
for TestStepMun in range(TestStepMunFlag):
TestResultFlag = self.numberLen(self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun],3)
# TestResultFlag1 = self.numberLen(self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][TestStepMun],4)
# print('TestResultFlag1+'+str(TestResultFlag1))
#预期结果不能为空
if TestResultFlag != 0:
self.caseDict['myTestCase']= self.XmindContent['title']
self.caseDict['TestCase'] = self.XmindContent['topics'][TestSuitMun]['title']
self.caseDict['Testprecondition'] = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['title']
self.caseDict['TestStep']=self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['title']
self.caseDict['TestResult'] = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['title']
# self.caseDict['TestResult'] = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['title']
# print(self.caseDict['TestResult'])
self.caseDict['Testbelong']='功能测试'
self.caseDict['Teststatus']='正常'#self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['makers'][0]
Testpriority1 = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['makers'][0]
if Testpriority1 == 'priority-1':
Testpriority= '高'
elif Testpriority1 == 'priority-2':
Testpriority= '中'
else:
Testpriority= '低'
self.caseDict['Testpriority']=Testpriority
# self.caseDict['Testpriority']=self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['makers'][0]
self.caseDict['Testoperator']=operator
# self.caseDict['myTestCase']= self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][TestStepMun]['topics'][0]['title']
#self.caseDict['TestSuit']+'/'+self.caseDict['TestCase']+'-'+self.caseDict['TestStep']+'-'+self.caseDict['TestResult']
# print(self.rowNum,len(self.caseDict['TestSuit']+'/'+self.caseDict['TestCase']+'-'+self.caseDict['TestStep']+'-'+self.caseDict['TestResult']),self.caseDict['TestSuit']+'/'+self.caseDict['TestCase']+'-'+self.caseDict['TestStep']+'-'+self.caseDict['TestResult'])
self.rowNum=self.rowNum+1
self.writeExcel(self.rowNum,self.caseDict,self.XmindTitle)
showinfo(title='转换结束', message='生成{0}条用例,请检查是否有误。'.format(self.rowNum))
if __name__ == '__main__':
XmindFile = os.path.join(os.path.dirname(__file__),'xmind_excel.xmind') # xmind文件
operator = 'dididdi'
xmind_to_csv().readXmind('C:\\Users\\Administrator\\Documents\\2测试案例点归档\\测试用例规范.xmind',operator)