import operator,wx,csv,os,datetime,unittest
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from HTMLTestRunner import HTMLTestRunner
class UI_test_frame_v4():
def __init__(self):
self.app=wx.App()
self.window=wx.Frame(None,title="测试框架",size=(400,300))
self.panel=wx.Panel(self.window)
self.filebq=wx.StaticText(self.panel,label="配置文件")
self.filetxt=wx.TextCtrl(self.panel,style=wx.TE_READONLY)
self.dakai=wx.Button(self.panel,label="打开")
self.zhixing=wx.Button(self.panel,label="执行")
self.chongzhi=wx.Button(self.panel,label="重置")
self.tuichu= wx.Button(self.panel,label="退出")
self.biaoge=wx.RadioButton(self.panel,-1,"表格形式报告")
self.HTML = wx.RadioButton(self.panel,-1,"HTML形式报告")
self.bug=wx.CheckBox(self.panel,label="生成bug报告")
self.bug.Show(False)
self.youjian=wx.CheckBox(self.panel,label="发送bug报告邮件")
self.youjian.Show(False)
def Box(self):
boxsizer1=wx.BoxSizer()
boxsizer1.Add(self.filebq,proportion=2,flag=wx.ALL,border=10)
boxsizer1.Add(self.filetxt,proportion=10,flag=wx.ALL,border=10)
boxsizer2 = wx.BoxSizer()
boxsizer2.Add(self.biaoge,flag=wx.ALL,border=10)
boxsizer2.Add(self.bug,flag=wx.ALL|wx.RESERVE_SPACE_EVEN_IF_HIDDEN,border=10)
boxsizer2.Add(self.youjian,flag=wx.ALL|wx.RESERVE_SPACE_EVEN_IF_HIDDEN,border=10)
boxsizer3 = wx.BoxSizer()
boxsizer3.Add(self.HTML,flag=wx.LEFT,border=10)
boxsizer4= wx.BoxSizer()
boxsizer4.Add(self.dakai,flag=wx.ALL,border=10)
boxsizer4.Add(self.zhixing,flag=wx.ALL,border=10)
boxsizer4.Add(self.chongzhi,flag=wx.ALL,border=10)
boxsizer4.Add(self.tuichu,flag=wx.ALL,border=10)
boxsizer5=wx.BoxSizer(wx.VERTICAL)
boxsizer5.Add(boxsizer1)
boxsizer5.Add(boxsizer2)
boxsizer5.Add(boxsizer3)
boxsizer5.Add(boxsizer4)
self.panel.SetSizer(boxsizer5)
def open_file(self,event):
self.dlog=wx.FileDialog(self.panel,message="打开文件",wildcard="*.csv",style=wx.FD_OPEN)
if self.dlog.ShowModal()==wx.ID_OK:
self.filetxt.AppendText(self.dlog.GetPath())
self.config=self.dlog.GetPath()
def run_driver(self,event):
filetxt=self.filetxt.GetValue()
if filetxt=="":
dlg=wx.MessageDialog(None,"请输入文件名和路径","提示信息",wx.YES_DEFAULT|wx.ICON_QUESTION)
if dlg.ShowModal()==wx.ID_YES:
dlg.Destroy()
else:
driver_obj=driver()
if self.biaoge.GetValue()==True:
if self.bug.GetValue()==True:
bugreport=True
if self.youjian.GetValue()==True:
send_bug=True
else:
bugreport=False
driver_obj.runtest(self.config,bugreport,send_bug)
elif self.HTML.GetValue()==True:
driver_obj.runtest1(self.config)
else:
dlg=wx.MessageDialog(None,"请选择报告类型","提示信息",wx.YES_DEFAULT|wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
dlg.Destroy()
def clear(self,event):
self.filetxt.SetValue("")
def exit(self,event):
self.window.Close()
def event_bind(self):
self.dakai.Bind(wx.EVT_BUTTON, self.open_file)
self.zhixing.Bind(wx.EVT_BUTTON, self.run_driver)
self.chongzhi.Bind(wx.EVT_BUTTON,self.clear)
self.tuichu.Bind(wx.EVT_BUTTON,self.exit)
self.biaoge.Bind(wx.EVT_RADIOBUTTON,self.xianshi_bug)
self.HTML.Bind(wx.EVT_RADIOBUTTON,self.yincang_bug)
self.bug.Bind(wx.EVT_CHECKBOX,self.xianshi_youjian)
def xianshi_bug(self,event):
if self.biaoge.GetValue()==True:
self.bug.Show(True)
def yincang_bug(self,event):
if self.HTML.GetValue()==True:
self.bug.Show(False)
self.youjian.Show(False)
def xianshi_youjian(self,event):
if self.bug.GetValue()==True:
self.youjian.Show(True)
else:
self.youjian.Show(False)
def run(self):
self.window.Show()
self.app.MainLoop()
class driver():
def runtest(self,config,bugreport,send_bug):
if bugreport==True:
bugobj=bug_report()
bugobj.create_bugreport()
starttime=datetime.datetime.now()
file=open(config,"r")
table=csv.reader(file)
reportobj=report()
reportobj.create_report()
header=next(table)
list=[]
line=0
sucnum=0
errnum=0
for row in table:
line+=1
dic={
}
dic["bianhao"]=row[0]
dic["lujing"]=row[1]
dic["state"]=row[2]
dic["num"] = int(row[3])
dic["youxiang"]=row[4]
dic['kaifa']=row[5]
list.append(dic)
list1=sorted(list,key=operator.itemgetter("num"))
for i in range(0,line):
n=0
for j in list1[i].items():
n+=1
if n==1:
bianhao=j[1]
if n==2:
lujing=j[1]
if n==3:
state=j[1]
if n==4:
num=j[1]
if n==5:
youxiang=j[1]
if n==6:
kaifa=j[1]
if state=="yes":
strOS=("python "+lujing)
os.system(strOS)
reportobj.read_result()
reportobj.writeconent(bianhao,lujing,state,num,youxiang,kaifa,reportobj.content)
sucnum=sucnum+int(reportobj.sucnum)
errnum=errnum+int(reportobj.errnum)
if int(reportobj.errnum)>0:
bugobj.read_bug()
bugobj.write_bug(bianhao,lujing,state,num,youxiang,kaifa,bugobj.result)
if send_bug==True:
send_obj=send_bug_email()
bugcontent=bugobj.result
email=youxiang
send_obj.send_email(bugcontent,email)
endtime=datetime.datetime.now()
difftime=(endtime-starttime).microseconds
reportobj.writetotal(sucnum,errnum,difftime)
print("报告已生成")
def runtest1(self,config):
file=open(config,"r")
table=csv.reader(file)
header=next(table)
list=[]
line=0
for row in table:
line+=1
dic={
}
dic[row[1]] = row[0]
dic["num"] = int(row[3])
dic["state"] = row[2]
list.append(dic)
list1=sorted(list, key=operator.itemgetter("num"))
for i in range(0,line):
n=0
for j in list1[i].items():
n+=1
if n==1:
fname = j[1]
route = j[0]
if n==3:
state=j[1]
if state=="yes":
suite=unittest.defaultTestLoader.discover(route,pattern=fname)
path=(r"E:\testframe\test_report\HTML_report.html")
file=open(path,"ab")
HTMLTestRunner(stream=file,verbosity=1,title="自动化测试报告",description="chrome",tester="张三").run(suite)
file.close()
class report():
def create_report(self):
self.path=r"E:\testframe\test_report\report.csv"
if os.path.exists(self.path):
os.remove(self.path)
self.reportfile=open(self.path,"a",newline="")
self.w=csv.writer(self.reportfile)
file=open(r"E:\testframe\test_config\test_config.csv","r")
table=csv.reader(file)
title=list(table)[0]
self.w.writerow(title+["测试结论"])
self.reportfile.close()
def writeconent(self,bianhao,jiaobenlujing,zhuangtai,xunxu,youxiang,kaifa,content):
self.reportfile=open(self.path,"a",newline="")
self.w=csv.writer(self.reportfile)
self.w.writerow([bianhao]+[jiaobenlujing]+[zhuangtai]+[xunxu]+[youxiang]+[kaifa]+[content])
self.reportfile.close()
def writetotal(self,sucnum,errnum,time):
self.reportfile=open(self.path,"a",newline="")
self.w=csv.writer(self.reportfile)
totalnum=sucnum+errnum
self.w.writerow(["测试用例总数"]+[totalnum]+["条"])
self.w.writerow(["执行成功用例数"]+[sucnum]+["条"])
self.w.writerow(["执行失败用例数"]+[errnum]+["条"])
self.w.writerow(["执行时间"]+[time]+["微秒"])
self.reportfile.close()
def add_result(self,content,sucnum,errnum):
tmp=open(r"E:\testframe\test_report\tmp.txt","w")
tmp.write(content+"\n")
tmp.write(sucnum+"\n")
tmp.write(errnum+"\n")
tmp.close()
def read_result(self):
tmp=open(r"E:\testframe\test_report\tmp.txt","r")
result=tmp.read().split("\n")
self.content=result[0]
self.sucnum=result[1]
self.errnum=result[2]
class bug_report():
def create_bugreport(self):
self.path = r"E:\testframe\bug_report\bugreport.csv"
if os.path.exists(self.path):
os.remove(self.path)
self.reportfile=open(self.path, "a", newline="")
self.w=csv.writer(self.reportfile)
file=open(r"E:\testframe\test_config\test_config.csv", "r")
table=csv.reader(file)
title=list(table)[0]
self.w.writerow(title+["未通过测试用例"])
self.reportfile.close()
def add_bug(self,content):
tmp=open(r"E:\testframe\bug_report\tmp.txt","w")
tmp.write(content)
tmp.close()
def read_bug(self):
tmp = open(r"E:\testframe\bug_report\tmp.txt","r")
self.result=tmp.read()
def write_bug(self,bianhao,jiaobenlujing,zhuangtai,xunxu,youxiang,kaifa,content):
self.reportfile=open(self.path,"a",newline="")
self.w=csv.writer(self.reportfile)
self.w.writerow([bianhao]+[jiaobenlujing]+[zhuangtai]+[xunxu]+[youxiang]+[kaifa]+[content])
self.reportfile.close()
class send_bug_email():
def send_email(self,bugcontent,email):
smtp=smtplib.SMTP("smtp.126.com")
smtp.login("[email protected]","YLHNHUWPQLAIERGF")
msg=MIMEText(bugcontent,"html","utf-8")
msg["subject"]=Header("bug报告")
msg["from"]="[email protected]"
msg["to"]=email
smtp.sendmail(msg["from"],msg["to"],msg.as_string())
smtp.close()
if __name__ == '__main__':
UI_test_frame_obj=UI_test_frame_v4()
UI_test_frame_obj.Box()
UI_test_frame_obj.event_bind()
UI_test_frame_obj.run()
配置文件: