PyTestReport Details:
https://testerhome.com/opensource_projects/78
Sample Code
# Coding: UTF-8 Import os, unittest, Time, HTMLTestRunner, smtplib from email.mime.multipart Import MimeMultipart from email.mime.text Import MimeText from config Import readconfig from pytestreport Import TestRunner # Import HTMLTestRunner_jpg # current script file path to true = os.path.dirname cur_path (the os.path.realpath ( __FILE__ )) Print (cur_path) # Print (of the type (cur_path)) # Step One: load method with discover all the test cases DEF add_case (caseName =" Case " , rule = " the Test * .py " ): '' ' The first step: load all test cases ' '' case_path = os.path.join (cur_path, caseName) # Use Case folder # If this does not exist case folder, it automatically creates an IF not os.path.exists (case_path): os.mkdir (case_path) Print ( " \ 033 [31mtest case path: \ 033 [0m% S " % case_path) # defined discover methods parameter Discover = unittest.defaultTestLoader.discover (case_path, pattern = rule, top_level_dir = None) Print (Discover) return Discover # Step: HTML report generation DEF run_case (all_case, the reportName = " Report " ): '' ' Step two: the implementation of all the use cases, and writes the result to the test HTML report '' ' now = time.strftime ( " % Y_% m_%% D_ H_% m_% S " ) report_path = os.path.join (cur_path, reportName) # test report folder # If the folder does not exist in this report, automatically create an IF not os.path.exists (report_path): os.mkdir (report_path) #= the os.path.join report_abspath (report_path, now + "and result.html") # with test report name jenkins generated fixed and result.html report_abspath = the os.path.join (report_path, " and result.html " ) Print ( " \ 033 [31mreport path: \ 033 [0m% S " % report_abspath) FP = Open (report_abspath, " WB " ) # PyTestReport test report with Open (report_abspath, ' WB ' ) AS FP: Runner = TestRunner (FP, title = ' test title ', The Description = ' test description ' , verbosity = 2 ) # calls to add the function returns the value runner.run (all_case) # Step Three: Get the latest test reports DEF get_report_file (report_path): '' ' The third step: Get the latest test report '' ' Lists = the os.listdir (report_path) lists.sort (Key = the lambda Fn: os.path.getmtime (the os.path.join (report_path, Fn))) Print (U ' latest test report generation: ' + Lists [-1 ]) #Find the latest test report file generated report_file = os.path.join (report_path, Lists [-1 ]) return report_file # Step Four: Send a test report to the mailbox DEF send_mail (SENDER, PSW, Receiver, smtpserver, report_file, Port) : '' ' step four: send the latest test report ' '' with Open (report_file, " rb " ) AS f: mail_body = f.read () # define message content msg = MimeMultipart () body = MimeText (mail_body , _subtype = ' HTML ' , _CHARSET = ' UTF-. 8 ' ) MSG [ 'Subject'] = '自动化测试报告' msg["from"] = sender msg["to"] = receiver msg.attach(body) # 添加附件 att = MIMEText(open(report_file,"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) # 用户名密码 smtp.login(sender,psw) smtp.sendmail(sender,receiver,msg.as_string()) smtp.quit() print('test report email has send out !') if __name__ == "__main__": all_case = add_case() # 1加载用例 # Test report generation path run_case (all_case) # 2 execution Example # obtain the latest test report file report_path = the os.path.join (cur_path, " Report " ) # with Example folder report_file = get_report_file (report_path) # . 3 to obtain the latest test report # mailbox configuration SENDER = readConfig.sender PSW = readConfig.psw smtp_server = readConfig.smtp_server Port = readConfig.port Receiver = readConfig.receiver # send_mail (sender, psw, receiver, smtp_server, report_file, port) # 4 The final step to send a report
Generated test report