Send the latest test report to the mail
Example:
ImportHtmltestrunnerImportUnitTestImportOsImportTimeImportSmtplibFrom Email.mime.textImportMimetextFrom Email.mime.multipartImportMimemultipartDefAllcase ():‘‘‘Load test Cases‘‘‘Discover =Unittest.defaultTestLoader.discover (Casepath, pattern="case*.py", top_level_dir=None)ReturnDiscoverDefRun_case ():‘‘‘Execute test cases, generate test reports‘‘‘now = Time.strftime ("%y_%m_%d%h_%m_%s") Htmlreportpath = Os.path.join (Reportpath, now+"Result.html") fp = open (Htmlreportpath,"Wb") Runner = Htmltestrunner.htmltestrunner (stream=FP, Title=u"Automated test reports", Description=u"Test Case execution")#Call the Allcase function return valueRunner.run (Allcase ()) Fp.close ()DefGet_report_file (Reportpath):‘‘‘Get the latest test report‘‘‘Lists =Os.listdir (Reportpath) Lists.sort (key=LambdaFn:os.path.getmtime (Os.path.join (Reportpath, FN)))#Find the latest generated report file Reportfile = Os.path.join (Reportpath, lists[-1])ReturnReportfiledef send_mail (sender, PSW, receiver, Smtpserver,reportfile, port=465):‘‘‘Send the latest test report content‘‘‘#Open test Report with open (Reportfile,"Rb") as F:mail_body =F.read ()#Define Message Content msg =Mimemultipart () BODY = Mimetext (Mail_body, _subtype=‘Html', _charset=‘Utf-8‘) msg[‘Subject'] = u"Automated test reports"msg["From"] =Sender msg["To"] =Receiver Msg.attach (body)#Add an attachment ATT = mimetext (open (Reportfile,"Rb"). Read (),"Base64","Utf-8") att["Content-type"] ="Application/octet-stream"att["Content-disposition"] =‘Attachment Filename= "Report.html"‘Msg.attach (ATT)Try: SMTP =Smtplib. Smtp_ssl (SmtpServer, Port)Except: SMTP =Smtplib. SMTP () smtp.connect (Smtpserver,port)#User name passwordSmtp.login (sender, PSW) smtp.sendmail (sender, receiver, msg.as_string ()) Smtp.quit ()If__name__ = =‘__main__‘: Casepath = Os.path.join (OS.GETCWD (),"Case")#Test Case Storage Path Reportpath = Os.path.join (OS.GETCWD (),"Report")#Test Report Storage Path Run_case ()# Execute test case reportfile = Get_report_file (Reportpath) SmtpServer = "smtp.qq.com " sender = " [email Protected] "# own account PSW = Span style= "COLOR: #800000" > "password" # own password receiver = " [email protected]" # each other's account Send_mail (sender, PSW, receiver, smtpserver,reportfile)
Python+selenium+unittest Test Framework 4-mail send the latest test report