第一步:用discover方法加載所有的測試用例
1.cur_path這個參數是讀取當前這個腳本的真實路徑,也就是run_main.py的真實路徑
2.caseName="case"這個case是存放測試用例的文件夾,如果沒有的話,自動創建。如果想運行其它文件夾的用例,就改下caseName這個參數值
3.rule="test*.py"這個是匹配用例腳本名稱的規則,默認匹配test開頭的所有用例
1 import unittest 2 import os 3 import time 4 import HTMLTestRunner 5 import smtplib 6 from email.mime.multipart import MIMEMultipart 7 from email.mime.text import MIMEText 8 9 10 #當前腳本所在文件真實路徑 11 cur_path = os.path.dirname(os.path.abspath(__file__)) 12 13 def add_case(caseName="case",rule="test*.py"): 14 '''第一步:加載所有的測試用例''' 15 case_path = os.path.join(cur_path,caseName) #用例文件夾 16 #定義discover方法的參數 17 discover = unittest.defaultTestLoader.discover(case_path, 18 pattern=rule, 19 top_level_dir=None) 20 return discover
第二步:生成HTML報告
1.把上一步加載到用例的參數傳入這個函數,測試報告的文件名稱默認report文件夾:reportName="report
2.如果沒有這個report文件夾也沒關系,可以自動創建的
1 def run_case(all_case,reportName="report"): 2 '''第二步:執行所有的用例,並把結果寫入HTML測試報告''' 3 now = time.strftime("%Y_%m_%d_%H_%M_%S") 4 report_path = os.path.join(cur_path,reportName) #報告文件夾 5 #如果不存在就創建 6 if not os.path.exists(report_path): 7 os.mkdir(report_path) 8 9 report_abspath = os.path.join(report_path,now+"result.html") 10 fp = open(report_abspath,"wb") 11 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, 12 title="自動化測試報告", 13 description="用例執行情況") 14 15 #調用add_case返回值 16 runner.run(all_case) 17 fp.close()
第三步:獲取最新的測試報告
1.如果第二步生成的測試報告加了時間戳,想找到最新的文件就用第三步
2.如果第二步不加時間戳,只是生成result.html,那這一步其實沒卵用,可以忽略
(個人覺得報告用一個名稱result.html就行,新的自動覆蓋舊的)
1 def get_report_file(report_path): 2 '''第三步:獲取最新的測試報告''' 3 lists = os.listdir(report_path) 4 lists.sort(key=lambda fn:os.path.getmtime(os.path.join(report_path,fn))) 5 print("最新測試報告: "+lists[-1]) 6 #找到最新生成的測試報告 7 report_file = os.path.join(report_path,lists[-1]) 8 return report_file
第四步:發送測試報告到郵箱
1.像QQ郵箱這種ssl加密的就走SMTP_SSL,用授權碼登錄
2.其它郵箱就正常賬號密碼登錄,走SMTP
1 def send_mail(sender,psw,receover,smtpserver,report_file,port): 2 '''第四步:發送最新的測試報告''' 3 with open(report_file,"rb") as f: 4 mail_body = f.read() 5 6 #定義郵件內容 7 msg = MIMEMultipart() 8 body =MIMEText(mail_body,_subtype="html",_charset="utf-8") 9 msg["Subject"] = "自動化測試報告" 10 msg["from"] = sender 11 msg["to"] = psw 12 msg.attach(body) 13 14 #添加附件 15 att = MIMEText(open(report_file,"rb").read(),"base64","utf-8") 16 att["Content-Type"] = "application/octet-stream" 17 att["Content-Disposition"] = "attachment; filename = 'report.html'" 18 msg.attach(att) 19 try: 20 smtp = smtplib.SMTP_SSL(smtpserver,port) 21 except: 22 smtp = smtplib.SMTP() 23 smtp.connect(smtpserver,port) 24 25 #用戶名密碼 26 smtp.login(sender,psw) 27 smtp.sendmail(sender,receover,msg.as_string()) 28 smtp.quit() 29 print("test report email has send out")
最后執行代碼
1.這里郵箱的內容讀的配置文件
1 if __name__ == "__main__": 2 all_case = add_case() # 1 加載用例 3 # 生成測試報告路徑 4 run_case(all_case) # 2 執行用例 5 report_path = os.path.join(cur_path,"report") # 用例文件 6 report_file = get_report_file(report_path) # 3 獲取最新測試報告 7 #郵箱配置 8 from config import readConfig 9 sender = readConfig.sender 10 psw = readConfig.psw 11 smtp_server = readConfig.smtp_server 12 port = readConfig.port 13 receiver = readConfig.receiver 14 send_mail(sender,psw,receiver,smtp_server,report_file,port) # 4 最后一步發送報告