GUI图形化自动化框架研发V4.0(发送缺陷报告至开发人员邮箱)

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("[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()

配置文件:
在这里插入图片描述
GUI图形化自动化框架研发V4.0(发送缺陷报告至开发人员邮箱)_第1张图片
GUI图形化自动化框架研发V4.0(发送缺陷报告至开发人员邮箱)_第2张图片
GUI图形化自动化框架研发V4.0(发送缺陷报告至开发人员邮箱)_第3张图片
GUI图形化自动化框架研发V4.0(发送缺陷报告至开发人员邮箱)_第4张图片

你可能感兴趣的:(测试工具开发,软件测试,python,经验分享)