Python+selenium+unittest Test Framework 3-project build and send mail

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.