GUI圖形化自動化框架研發V4.0(傳送缺陷報告至開發人員郵箱)

zou75718發表於2020-10-10
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形式報告")
        #定義核取方塊bug報告
        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):
        #設定一個水平的BoxSizer
        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)
        #設定第二個水平的BoxSizer
        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)
        # 設定第三個水平的BoxSizer
        boxsizer3 = wx.BoxSizer()
        boxsizer3.Add(self.HTML,flag=wx.LEFT,border=10)
        #設定第s四個水平的BoxSizer
        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)
        # 設定垂直的BoxSizer
        boxsizer5=wx.BoxSizer(wx.VERTICAL)
        boxsizer5.Add(boxsizer1)
        boxsizer5.Add(boxsizer2)
        boxsizer5.Add(boxsizer3)
        boxsizer5.Add(boxsizer4)
        #設定boxsizer生效
        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:
                #判斷是否生成bug報告
                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)
    #顯示生成bug報告按鈕事件
    def xianshi_bug(self,event):
        if self.biaoge.GetValue()==True:
            self.bug.Show(True)

    # 隱藏生成bug報告按鈕事件
    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()
        # 執行APP
        self.app.MainLoop()

#測試框架驅動類
class driver():
    #測試驅動執行方法
    def runtest(self,config,bugreport,send_bug):
        if bugreport==True:
            #建立bug報告
            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"))
        #print(list1)
        for i in range(0,line):
            n=0
            for j in list1[i].items():
                #print(j)
                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)
                        #寫入bug報告
                        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:
            #print(row)
            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():
                #print(j)
                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]
        #print(title)
        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]

#bug報告類
class bug_report():
    # 建立bug報告寫入標題欄
    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]
        # print(title)
        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()

#傳送bug郵件類
class send_bug_email():
    def send_email(self,bugcontent,email):
        #設定郵件伺服器
        smtp=smtplib.SMTP("smtp.126.com")
        #登入郵箱
        smtp.login("zou126@126.com","YLHNHUWPQLAIERGF")
        #建立郵件物件,並設定郵件正文
        msg=MIMEText(bugcontent,"html","utf-8")
        #建立郵件標題
        msg["subject"]=Header("bug報告")
        #指定發件人
        msg["from"]="zou126@126.com"
        #指定收件人
        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()

配置檔案:
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

相關文章