单元UI自动化测试脚本之上的脚本组织结构


#all_test.py


#
coding:UTF-8 import unittest import HTMLTestRunner import time import smtplib from email.mime.text import MIMEText import os #===================定义发送邮件======================== def send_mail(file_new): #发送邮箱 mail_from='qq413452039@126.com' #收信邮箱 mail_to='paul.wang@bindo.com' #定义正文 f=open(file_new,'rb') mail_body=f.read() f.close() msg=MIMEText(mail_body,_subtype='html',_charset='utf8') #定义标题 msg['Subject']=u'自动化测试报告' #定义发送时间(不定义的可能有的邮件客户端会不显示发送时间) msg['date']=time.strftime('%a,%d %b %Y %H:%M:%S %z') msg['From']=mail_from msg['To']=mail_to smtp=smtplib.SMTP() #连接SMTP服务器,此处用的是126的SMTP服务器 smtp.connect('smtp.126.com') #用户名密码 smtp.login('qq413452039@126.com','w1111111')#设置对应邮箱的SMTP服务授权码,这个授权码替代代码中邮箱的登录密码,即可成功发送邮件 smtp.sendmail(mail_from,mail_to,msg.as_string()) smtp.quit() print(u'email has send out!!!') #==========查找测试报告目录,找到最新生成的测试报告文件======== def send_report(testreport): result_dir=testreport lists=os.listdir(result_dir) lists.sort(key=lambda fn:os.path.getmtime(testreport+fn)) print(lists) #print (u'最新测试生成的报告:'+lists[-1]) #找到最新生成的文件 file_new=os.path.join(result_dir,lists[-1]) print(file_new) #调用发邮件模块 send_mail(file_new) #=================将测试用例添加到测试套件========================= def creatsuite(): testunit=unittest.TestSuite() #定义测试文件查找目录 test_dir=r'/Users/bindo/Documents/' #定义discover方法的参数 discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py',top_level_dir=None) #discover方法筛选出来的用例,循环添加到测试套件中 for test_case in discover: print(test_case) testunit.addTests(test_case) return testunit if __name__=='__main__': now=time.strftime('%Y-%m-%d %H_%M_%S') testreport=r'/Users/bindo/Documents/report/' filename=testreport+now+'result.html' fp=open(filename,'wb') runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'自动化测试报告',description=u'用例执行情况:') alltestnames=creatsuite() runner.run(alltestnames) fp.close()#关闭生成的报告 send_report(testreport) # now=time.strftime('%Y-%m-%d %H_%M_%S') # filename='D:\\report\\'+now+'result.html' # fp=open(filename,'wb') # runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'测试报告',description=u'用例执行情况:') # if __name__=='__main__': # alltestnames=creatsuite() # runner.run(alltestnames) # fp.close()
#start_run.py
import
os import time k=1 while k<2: now_time=time.strftime('%H:%M:%S') if now_time=='18:00:00': print(u'开始运行脚本:') os.chdir('/Users/bindo/Documents/') os.system('python3 all_test.py') print(u'运行完成退出') break else: time.sleep(10) print(now_time)

 

posted @ 2018-10-30 15:13  paulwang2018  阅读(600)  评论(0编辑  收藏  举报