Project build and send mail
I. Construction of the project
1, set up the project Chen
Open the upper-left corner of Pycharm file>new project, select Opin in the current window after creating the folder D:\chen where you entered the testing project.
2. Create sub-folders
PS: Create a folder, be sure to select the Python package to create the way.
3. Create a test script
4. Create runalltest.py
PS: In the runalltest.py script, write the main function, control the execution of all the use cases.
5. Download the source code to generate the test report
ImportHtmltestrunnerImportUnitTestImportOs#Test Case Storage Path Casepath = Os.path.join (OS.GETCWD (),"Case")#Test Report Storage Path Reportpath = Os.path.join (OS.GETCWD (),"Report")DefAllcase ():‘‘‘Load test Cases‘‘‘Discover =Unittest.defaultTestLoader.discover (Casepath, pattern="case*.py", top_level_dir=None)ReturnDiscoverDefRuncase ():‘‘‘Execute test cases, generate test reports‘‘‘Htmlreportpath = Os.path.join (Reportpath,"Result.html") fp = open (Htmlreportpath, "wb" ) Runner = Htmltestrunner.htmltestrunner (Stream=FP, Title=u " automated test report " " test case execution " ) # Call allcase function return value Runner.run (Allcase ()) fp.close () if __name__ = = "__main__ ": Runcase ()
Second, send the mail
ImportSmtplibFrom Email.mime.textImportMimetextFrom Email.mime.multipartImportMimemultipartdef 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__‘: reportfile = u"F:\\python36\\test\\report\\result.html"# test report Path SmtpServer = smtp.qq.com "# mailbox server sender = " [email protected]< Span style= "COLOR: #800000" > "# own account PSW = " password# own password receiver = [email protected] "# each other's account Send_mail (sender, PSW, receiver, smtpserver,reportfile)
Python+selenium+unittest Test Framework 3-project build and send mail