From HtmltestrunnerImport Htmltestrunner
From Email.mime.textImport Mimetext
From Email.headerImport Header
Import Smtplib
Import UnitTest
Import time
Import OS
#---------Define Send Message--------------#
DefSend_mail (file_new):
f =Open (file_new,' RB ')
Mail_body = F.read ()
F.close ()
# Send Mailbox Server
SmtpServer =' Smtp.163.com '
# Send email user/password
user =' [Email protected] '
Password =' Ren313123 '
# Send Mailbox
Sender =' [Email protected] '
# Receive Mailboxes
Receiver =' [Email protected] ',' [Email protected] '
#receiver = ' [email protected] '
# msg = Mimetext (mail_body, ' html ', ' Utf-8 ')
msg = Mimetext (mail_body,_subtype =' HTML ',_charset =' Utf-8 ')
# Send Message subject
Subject =' Automated test Report '
msg[' From ' = Sender
msg[' to '] =', '. Join (receiver)
# Write HTML-type message body
msg[' Subject ' = Header (Subject,' Utf-8 ')
# Connect to send mail
SMTP = Smtplib. SMTP ()
Smtp.connect (SmtpServer)
Smtp.login (user, password)
Smtp.sendmail (Sender, receiver, Msg.as_string ())
Smtp.quit ()
Print' Email has send out! ')
#------------Find the test report directory and find the latest generated test report file
DefNew_report (Testreport):
Lists = Os.listdir (Testreport)
Lists.sort (key=Lambda Fn:os.path.getatime (Testreport +"\\"+ fn))
File_new = Os.path.join (testreport, lists[-1])
Print (file_new)
Return file_new
if __name__ = =' __main__ ':
Location of the #.py file package
Test_dir =C\\Users\\Dingce\\Pycharmprojects\\Untitled2\\Ecshoppackage '
#HTML报告的位置
Test_report =E\\‘
#运行指定py文件s生成以下的报告
Discover = Unittest.defaultTestLoader.discover (test_dir,pattern=' suiyi*.py ')
#时间
now = Time.strftime ("%y-%m-%d_%h_%m_%s")
filename = Test_report +‘\\' + Now + ' result.html '
FP = Open (Filename, "WB")
Runner = Htmltestrunner (stream=fp,title=,description=" use case execution: ')
Runner.run (Discover)
Fp.close ()
New_report = New_report (test_report)
Send_mail ( New_report) #发送测试报告
#整个程序的执行过程可以分为三个步骤
#1. A matching test case is found by Discover () of the UnitTest framework, and the Htmltestrunner run () method executes the test case and generates the latest test report
#2. Call the New_report () function to find the most recently generated test report in the test report directory (e-disk), and return the path to the test report
Generate HTML reports and consolidate auto-launch messages