Python3+HTMLTestRunner+SMTP生成测试报告后发送邮件
生活随笔
收集整理的這篇文章主要介紹了
Python3+HTMLTestRunner+SMTP生成测试报告后发送邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在前一篇https://www.cnblogs.com/zhengyihan1216/p/11549820.html 中記錄了如何生成html格式的報告,
這篇記錄下怎么將測試報告通過郵件發出
1、對test_add_dele.py文件進行修改及完善
注釋:email庫定義郵件里的內容,smtplib庫進行郵件發送
#coding=utf-8
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import unittest
import add_dele
import time
import os #在測試報告目錄下找到最新的報告文件,打印且返回最新報告的名稱
def find_new_report(report_dirc):
lists = os.listdir(report_dirc)
lists.sort(key=lambda fn:os.path.getmtime(report_dirc+"\\"+fn))
new_report = os.path.join(report_dirc,lists[-1])
print(new_report)
return new_report def send_mail(new_report):
#讀取報告
f = open(new_report,'rb')
mail_body = f.read()
f.close()
#定義郵件正文,報告以正文的形式發送
msg = MIMEText(mail_body,'html','utf-8') #定義郵件標題
msg['Subject'] = Header("自動化測試報告","utf-8")
mail_server = "mail.xxxxx.com"
user_name = "username1@xxxxx.com"
pwd = "xxxxx"
from_add = "username1@xxxxx.com"
to_add = "username2@xxxxx.com" smtp = smtplib.SMTP()
smtp.connect(mail_server)
smtp.login(user_name,pwd)
smtp.sendmail(from_add,to_add,msg.as_string())
smtp.quit()
print("emial has send out") if __name__ =="__main__":
#構建測試集
suit = unittest.TestSuite()
#測試集加入add_dele文件中被調用的方法。格式suit.addTest(文件名.類名("方法名"))
suit.addTest(add_dele.Test_test("test_shuzi"))
suit.addTest(add_dele.Test_test("test_liangmethod"))
#定義存放測試報告的路徑及文件名
#我定義的路徑是:當前路徑+存放報告的專有目錄Report+文件名(文件名是當前時間+report.html)
curent_dirc=os.path.dirname(os.path.realpath(__file__))
report_dirc = curent_dirc + "\Report"
now = time.strftime("%Y%m%d-%H%M%S")
report_name = report_dirc+"\\"+now+"report.html"
fp = open(report_name,"wb")
runner = HTMLTestRunner(stream=fp,
title="測試一下報告生成",
description="用兩個數字的相加減來練習")
runner.run(suit)
fp.close()
#發送郵件
send_mail(find_new_report(report_dirc))
2、username2@xxxxx.com 該郵箱中收到的報告如下截圖所示:
3、注意問題
如果發出的測試報告里測試用例沒有展開,則將.....Python35\Lib\HTMLTestRunner.py文件中的改行刪除: .hiddenRow { display: none; }
由于大家用的HTMLTestRunner.py文件版本不同,所以該文件具體在第幾行不同,可以根據文字進行搜索后刪除。
總結
以上是生活随笔為你收集整理的Python3+HTMLTestRunner+SMTP生成测试报告后发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Asp.Net 中 HTTP 和 HTT
- 下一篇: 《Web接口开发与自动化测试》学习笔记(