python+selenium+unittest測試架構3-項目構建和發送郵件

來源:互聯網
上載者:User

標籤:控制   ade   def   伺服器   arm   UNC   runner   with open   default   

 項目構建和發送郵件

 

一、項目構建

1、建立項目chen

開啟pycharm左上方File>New Project,在Location輸入testing項目所在檔案夾D:\chen,建立後選擇Opin in current window。

2、建立子檔案夾

PS:建立檔案夾,一定要選Python Package的方式建立。

3、建立測試指令碼

 

4、建立runalltest.py

 PS:在runalltest.py這個指令碼裡面寫主函數,控制執行所有的用例。

5、下載產生測試報告的源碼

import HTMLTestRunnerimport unittestimport os#測試案例存放路徑casepath = os.path.join(os.getcwd(),"case")#測試報告存放路徑reportpath = os.path.join(os.getcwd(),"report")def allcase():    ‘‘‘載入測試用例‘‘‘    discover = unittest.defaultTestLoader.discover(casepath,                                                   pattern="case*.py",                                                   top_level_dir=None)    return discoverdef runcase():    ‘‘‘執行測試案例,產生測試報告‘‘‘    htmlreportpath = os.path.join(reportpath,"result.html")    fp = open(htmlreportpath,"wb")    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,                                           title=u"自動化測試報告",                                           description=u"測試案例執行情況")    # 調用allcase函數傳回值    runner.run(allcase())    fp.close()if __name__ == "__main__":    runcase()    

 

二、發送郵件

import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartdef send_mail(sender, psw, receiver, smtpserver,reportfile, port=465):    ‘‘‘發送最新的測試報告內容‘‘‘    #開啟測試報告    with open(reportfile, "rb") as f:        mail_body = f.read()    # 定義郵件內容    msg = MIMEMultipart()    body = MIMEText(mail_body, _subtype=‘html‘, _charset=‘utf-8‘)    msg[‘Subject‘] = u"自動化測試報告"    msg["from"] = sender    msg["to"] = receiver    msg.attach(body)    # 添加附件    att = MIMEText(open(reportfile, "rb").read(), "base64", "utf-8")    att["Content-Type"] = "application/octet-stream"    att["Content-Disposition"] = ‘attachment; filename= "report.html"‘    msg.attach(att)    try:        smtp = smtplib.SMTP_SSL(smtpserver, port)    except:        smtp = smtplib.SMTP()        smtp.connect(smtpserver,port)    # 使用者名稱密碼    smtp.login(sender, psw)    smtp.sendmail(sender, receiver, msg.as_string())    smtp.quit()if __name__ == ‘__main__‘:    reportfile = u"F:\\python36\\test\\report\\result.html"#測試報告路徑    smtpserver = "smtp.qq.com"  #  郵箱伺服器    sender = "[email protected]" # 自己的帳號    psw = "password" #自己的密碼    receiver = "[email protected]" #對方的帳號    send_mail(sender, psw, receiver, smtpserver,reportfile)

python+selenium+unittest測試架構3-項目構建和發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.