TestRunner.py發送本地地址
import HTMLTestRunner
import os
import unittest
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 設置報告文件保存路徑
report_path = os.path.dirname(os.path.abspath('.')) + '/test_report/'
# 獲取系統當前時間
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# 設置報告名稱格式
HtmlFile = report_path + now + "HTMLtemplate.html"
fp = open(HtmlFile, "wb")
# 發送到郵箱
#郵箱服務器
smtpserver = 'smtp.163.com'
#發送郵箱
sender = '[email protected]'
#發送郵箱的賬號密碼
username = '[email protected]'
password = 'password'
#發送郵件主題
subject = '自動化測試報告'
#接收郵箱
receiver = '[email protected]'
#文件內容
msg = MIMEText( HtmlFile,'html','utf-8')
msg['Subject'] = Header(subject,'utf-8')
msg['from'] = '[email protected]'
msg['to'] = '[email protected]'
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login('[email protected]','password')
a1=smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
# 構建suite
listaa = 'C:\\Users\suxiahua\PycharmProjects\\untitled\\automation_framework_demo\\testsuits'
def creatsuite1():
testunit = unittest.TestSuite()
# discover 方法定義
discover = unittest.defaultTestLoader.discover(listaa, pattern='test_*.py', top_level_dir=None)
# discover 方法篩選出來的用例,循環添加到測試套件中
for test_suite in discover:
for test_case in test_suite:
testunit.addTests(test_case)
print(testunit)
return testunit
alltestnames = creatsuite1()
if __name__ == '__main__':
# 初始化一個HTMLTestRunner實例對象,用來生成報告
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"某某項目測試報告", description=u"用例測試情況")
# 開始執行測試套件
runner.run(alltestnames)
另一種發送報告內容
import HTMLTestRunner
import os
import unittest
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def report():
# 設置報告文件保存路徑
report_path = os.path.dirname(os.path.abspath('.')) + '/test_report/'
# 獲取系統當前時間
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# 設置報告名稱格式
HtmlFile = report_path + now +"HTMLtemplate.html"
fp = open(HtmlFile, "wb")
return fp,HtmlFile
# 發送到郵箱
def send_mail(HtmlFile):
#郵箱服務器
smtpserver = 'smtp.163.com'
#發送郵箱
sender = '[email protected]'
#發送郵箱的賬號密碼
username = '[email protected]'
password = 'password'
#發送郵件主題
subject = '自動化測試報告'
#接收郵箱
receiver = '[email protected]'
#文件內容
f = open(HtmlFile, 'rb')
mail_body = f.read()
mail_body=mail_body.decode('utf8')
f.close()
msg = MIMEText(mail_body,'html','utf-8')
msg['Subject'] = Header(subject,'utf-8')
msg['from'] = '[email protected]'
msg['to'] = '[email protected]'
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
# 構建suite
listaa = 'C:\\Users\suxiahua\PycharmProjects\\untitled\\automation_framework_demo\\testsuits'
def creatsuite1():
testunit = unittest.TestSuite()
# discover 方法定義
discover = unittest.defaultTestLoader.discover(listaa, pattern='test_baidu_search1.py', top_level_dir=None)
# discover 方法篩選出來的用例,循環添加到測試套件中
for test_suite in discover:
for test_case in test_suite:
testunit.addTests(test_case)
print(testunit)
return testunit
alltestnames = creatsuite1()
if __name__ == '__main__':
fp,HtmlFile1 = report()
# 初始化一個HTMLTestRunner實例對象,用來生成報告
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"某某項目測試報告", description=u"用例測試情況")
# 開始執行測試套件
runner.run(alltestnames)
fp.close()
send_mail(HtmlFile1)