python + unittest logs, reports, e-mail output module

1, the log output module:

!. 1 # / usr / bin / Python the env 
 2 # - * - Coding: UTF-. 8 - * - 
 . 3 # @time: 2019/11/21 17:41 
 . 4 # @Site: 
 . 5 # @file: log.py 
 . 6 @Software #: PyCharm 
 . 7 
 . 8 Import Time 
 . 9 Import the logging 
10 
. 11 class logOutput (): 
12 is DEF logOutput (Self, LOG_DIR, name_project):  13 is' ''  14: param LOG_DIR: log path  15: param name_project: project name => logs are named for  16: return:  . 17 '' '  18 is # sys.path.append (the os.chdir (' ../ log '))  . 19 The time.strftime = now ( "% D%% Y_% m_Low H_% of M_ S% " )  20 is the logging.basicConfig(level=logging.DEBUG, 21 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 22 datefmt='%a, %d %b %Y %H:%M:%S', 23 filename=log_dir+ now +'-'+name_project+'_test_log.log', 24 filemode='w') 25 logger = logging.getLogger() 26 logger.info(self)

2, the report output modules:

!. 1 # / usr / bin / Python the env 
 2 # - * - Coding: UTF-. 8 - * - 
 . 3 # @time: 2019/11/21 17:44 
 . 4 # @Site: 
 . 5 # @file: report.py 
 . 6 # @Software: PyCharm 
 . 7 
 . 8 Import Time 
 . 9 Import the unittest 
10 from BSTestRunner Import BSTestRunner 
. 11 
12 is class reportOutput ():  13 is DEF reportOutput (Self, test_dir, report_dir, name_project):  14 '' '  15: param test_dir: using Example path  16: param report_dir: report path  17: param name_project: project name => name and description for the report  18 is: return:  . 19 '' ' The time.strftime now = 20 is ( "% D%% Y_% m_Low H_% of M_% S" ) = 21 is unittest.defaultTestLoader.discover Discover (test_dir, pattern = '* Test .py') loading test # 22 report_name = report_dir + now + '-' + name_project + '_ test_report.html' # report Title 23 with open (report_name, 'wb ') as f: ## run case generation test report 24 Runner = BSTestRunner (Stream = F, 25 title = name_project + ' testing the Report UIAuto_Regression ' , 26 is Description = (the U-+ "the UI automation regression test" name_project ), the verbosity = 27 2 ) 28 runner.run (Discover) 29 f.close ()

3, send e-mail report module:

 

! # / usr / bin / env Python 
# - * - Coding: UTF-8 - * - 
# @time: 2019/12/27 14:24 
# @Site: 
# @file: send_email.py 
# @Software: PyCharm 

Import OS, SYS 
Import smtplib 
Import Time 
from email.mime.text Import MimeText  from email.mime.multipart Import MimeMultipart 
 # = os.path.dirname path (os.path.abspath with ( '.'))  # = report_path path + '\\ \\ Report ' 
 class SEND_EMAIL (): DEF get_newreport (Self, report_path): = the os.listdir dirs (report_path) # obtain all the files dirs.sort path () # sort the list, the file acquired Reverse - collation, reverse = True descending, reverse = False ascending (default) newreportname = dirs [-1] # Print ( 'The new report name: {0}'. Format (newreportname)) return newreportname # test report is returned name def take_messages (self, report_path): # = write messages new_report self.get_newreport (report_path) Self = a .msg MimeMultipart () self.msg [ 'the Subject'] = 'automatic test report the BBA the JIT' # mail header self.msg [ 'date'] = time.strftime ( '% a,% d% b% Y% H:% M:% S% Z ' ) with Open (the os.path.join (report_path, new_report),' RB ' ) AS F: mailbody reached, f.read = () # read the contents of the test report html = MIMEText ( mailbody, _subtype = 'html', _charset = 'utf-8') # test report on the content of the message body which self.msg.attach (html) # msg in the appended html # html annex below is a test transmitting the report on the attachment = MimeText the ATTl (mailbody, 'Base64', 'UTF-. 8' ) the ATTl [ "the Type-the Content"] = 'file application / OCTET-Stream' the ATTl [ "the Content-Disposition"] = 'Attachment; filename = "TestReport.html"' # where filename can be any write, what to write the name, the name of the attachment is whatself.msg.attach (the ATTl) DEF SEND_EMAIL (Self): # recipients = [ '[email protected]', '[email protected]', '[email protected]'] # sent to multiple people recipients = [ '[email protected]'] # is sent to a person self.take_messages () self.msg [ 'from' ] = '[email protected]' # sender of the email, which is the company mail forwarding # self.msg [ 'to'] = recipients # recipients and senders must define it here, it will not perform error. = toaddrs Recipients SMTP = smtplib.SMTP () smtp.connect ( 'smtp.from.com' ) smtp.ehlo () smtp.login ('[email protected] ',' email_pwd ' ) smtp.sendmail (self.msg [ 'from'], toaddrs, self.msg.as_string ()) # E-mail smtp.close () Print ( 'the sendmail Success' ) IF the __name__ == '__main__' : SENDER = SEND_EMAIL () sender.send_email () # print (sender.

 

 

 

 

4, call the method and output files:

Directory Structure:

Call the method:

Output Style

Guess you like

Origin www.cnblogs.com/East-fence/p/12112300.html