Python unittest 自動(dòng)識(shí)別并執(zhí)行測(cè)試用例方式
自動(dòng)化測(cè)試執(zhí)行的用例有很多,python額測(cè)試用例文件,都是以“test”開(kāi)頭的。
TestLoader(defaultTestLoader)是unittest的測(cè)試用例加載器,它包括多個(gè)加載測(cè)試用例的方法。它的結(jié)果是返回一個(gè)測(cè)試套件。本文介紹discover()用法與功能
結(jié)構(gòu):
discover(start_dir, pattern=’test*.py’, top_level_dir=None)
作用:找到指定目錄下所有測(cè)試用例模塊,并遞歸查詢子目錄下的測(cè)試模塊,找到匹配的文件進(jìn)行加載。
解釋:
start_dir:需要測(cè)試的用例文件目錄或是模塊
pattern:用例匹配原則
top_level_dir:測(cè)試模塊的頂層目錄,沒(méi)有就默認(rèn)None。
例子:
#coding=utf-8import unittest #定義測(cè)試用例的目錄為當(dāng)前目錄test_dir = ’./’discover = unittest.defaultTestLoader.discover(test_dir, pattern=’test*.py’) if __name__ == ’__main__’: runner = unittest.TextTestRunner() runner.run(discover)
注釋:
1)discover = unittest.defaultTestLoader.discover(test_dir, pattern=’test*.py’) :匹配查找測(cè)試用例文件,以test*.py開(kāi)頭,并將查找到的測(cè)試用例組裝到測(cè)試套件中
2)runner.run(discover) :通過(guò)run()函數(shù)執(zhí)行discover
補(bǔ)充知識(shí):unittest框架執(zhí)行測(cè)試并發(fā)送郵件
我就廢話不多說(shuō)了,還是直接看代碼吧!
#coding=utf8 import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom HTMLTestRunner import HTMLTestRunnerfrom email.header import Headerimport unittestimport time,os #==============定義發(fā)送郵件 =============== def send_mail(file_new): f = open(file_new,’rb’) #讀取測(cè)試報(bào)告正文 mail_body = f.read() f.close() #發(fā)送郵件的 smtpserver = ’smtp.exmail.qq.com’ username = ’[email protected]’ passwd = ’Fyf136066’ sender = ’[email protected]’ receiver = [’[email protected]’] tname = time.strftime(’%Y-%m-%d %H-%M-%S’,time.localtime()) header = u’%s 接口自動(dòng)化測(cè)試報(bào)告 ’ % tname # 只發(fā)正文,不發(fā)附件 msg = MIMEText(mail_body, ’html’, ’utf-8’) msg[’Subject’] = Header(’自動(dòng)化測(cè)試報(bào)告’, ’utf-8’) msg[’Header’] = header msg[’From’] = sender msg[’To’] = ','.join(receiver) #連接發(fā)送郵件 # 發(fā)送郵件,端口用465, keyfile = ’vxkdfejinpifbeaj’ smtp = smtplib.SMTP_SSL(smtpserver, 465) smtp.helo(smtpserver) smtp.ehlo(smtpserver) smtp.login(username, passwd) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() #======================查找最新的測(cè)試報(bào)告========================== def new_report(testreport): #方式1: # lists = os.listdir(testreport) # lists.sort(key = lambda fn: os.path.getmtime(testreport + ’’ + fn)) # file_new = os.path.join(testreport,lists[-1]) # print(file_new) # return file_new #方式2: dirs = os.listdir(testreport) dirs.sort() newreportname = dirs[-1] print(’The new report name: {0}’.format(newreportname)) file_new = os.path.join(testreport, newreportname) return file_new if __name__ == ’__main__’: #獲取當(dāng)前的項(xiàng)目目錄UskidInterface testdir = os.path.dirname(os.path.dirname(__file__)) test_dir = os.path.join(testdir,’testcase’) test_report = os.path.join(testdir, ’report’) discover = unittest.defaultTestLoader.discover(test_dir,pattern=’test*.py’) now = time.strftime('%Y-%m-%d %H_%M_%S',time.localtime()) filename = test_report+’/result_’+now+’.html’ fp = open(filename,’wb’) #stream放生成報(bào)告的路徑 runner = HTMLTestRunner(stream=fp,title='測(cè)試報(bào)告',description=’用例執(zhí)行情況:’) runner.run(discover) fp.close() new_report = new_report(test_report) send_mail(new_report)
以上這篇Python unittest 自動(dòng)識(shí)別并執(zhí)行測(cè)試用例方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))4. 詳細(xì)分析css float 屬性以及position:absolute 的區(qū)別5. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)6. 得到XML文檔大小的方法7. 詳解JSP 內(nèi)置對(duì)象request常見(jiàn)用法8. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. ASP基礎(chǔ)知識(shí)Command對(duì)象講解
