欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python unittest 自動識別并執(zhí)行測試用例方式

 更新時間:2020年03月09日 14:58:13   作者:釋夢燃  
這篇文章主要介紹了Python unittest 自動識別并執(zhí)行測試用例方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

自動化測試執(zhí)行的用例有很多,python額測試用例文件,都是以“test”開頭的。

TestLoader(defaultTestLoader)是unittest的測試用例加載器,它包括多個加載測試用例的方法。它的結(jié)果是返回一個測試套件。本文介紹discover()用法與功能

結(jié)構(gòu):

discover(start_dir, pattern='test*.py', top_level_dir=None)

作用:找到指定目錄下所有測試用例模塊,并遞歸查詢子目錄下的測試模塊,找到匹配的文件進行加載。

解釋:

start_dir:需要測試的用例文件目錄或是模塊

pattern:用例匹配原則

top_level_dir:測試模塊的頂層目錄,沒有就默認(rèn)None。

例子:

#coding=utf-8
import unittest
 
#定義測試用例的目錄為當(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') :匹配查找測試用例文件,以test*.py開頭,并將查找到的測試用例組裝到測試套件中

2)runner.run(discover) :通過run()函數(shù)執(zhí)行discover

補充知識:unittest框架執(zhí)行測試并發(fā)送郵件

我就廢話不多說了,還是直接看代碼吧!

#coding=utf8
 
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from HTMLTestRunner import HTMLTestRunner
from email.header import Header
import unittest
import time,os
 
#==============定義發(fā)送郵件 ===============
 
def send_mail(file_new):
 f = open(file_new,'rb')
 #讀取測試報告正文
 mail_body = f.read()
 f.close()
 
 #發(fā)送郵件的
 smtpserver = 'smtp.exmail.qq.com'
 
 username = 'fengyanfang@innobuddy.com'
 passwd = 'Fyf136066'
 
 sender = 'fengyanfang@innobuddy.com'
 receiver = ['fengyanfang@innobuddy.com']
 tname = time.strftime('%Y-%m-%d %H-%M-%S',time.localtime())
 header = u'%s 接口自動化測試報告 ' % tname
 
 
# 只發(fā)正文,不發(fā)附件
 msg = MIMEText(mail_body, 'html', 'utf-8')
 msg['Subject'] = Header('自動化測試報告', '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()
 
 
#======================查找最新的測試報告==========================
 
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)前的項目目錄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放生成報告的路徑
 runner = HTMLTestRunner(stream=fp,title="測試報告",description='用例執(zhí)行情況:')
 runner.run(discover)
 fp.close()
 
 new_report = new_report(test_report)
 send_mail(new_report)

以上這篇Python unittest 自動識別并執(zhí)行測試用例方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 查看Django和flask版本的方法

    查看Django和flask版本的方法

    今天小編就為大家分享一篇查看Django和flask版本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python中opencv?Canny邊緣檢測

    python中opencv?Canny邊緣檢測

    這篇文章主要介紹了python中opencv?Canny邊緣檢測,Canny邊緣檢測是一種使用多級邊緣檢測算法檢測邊緣的方法。OpenCV提供了函數(shù)cv2.Canny()實現(xiàn)Canny邊緣檢測。更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • python中pip的安裝與使用教程

    python中pip的安裝與使用教程

    在安裝pip前,請確認(rèn)win系統(tǒng)中已經(jīng)安裝好了python,和easy_install工具,下面腳本之家小編給大家詳細(xì)介紹python中pip的安裝與使用教程,感興趣的朋友一起看看吧
    2018-08-08
  • 舉例詳解Python中循環(huán)語句的嵌套使用

    舉例詳解Python中循環(huán)語句的嵌套使用

    這篇文章主要介紹了舉例詳解Python中循環(huán)語句的嵌套使用,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法示例

    Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法示例

    這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法,結(jié)合具體實例形式分析了Python哈夫曼樹的原理、定義及簡單使用方法,需要的朋友可以參考下
    2018-04-04
  • python xlsxwriter模塊的使用

    python xlsxwriter模塊的使用

    這篇文章主要介紹了python xlsxwriter模塊的使用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • python web框架學(xué)習(xí)筆記

    python web框架學(xué)習(xí)筆記

    這篇文章主要為大家分享了python web框架學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 我喜歡你 抖音表白程序python版

    我喜歡你 抖音表白程序python版

    我喜歡你!這篇文章主要為大家詳細(xì)介紹了抖音表白程序python版的實現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python twilio模塊實現(xiàn)發(fā)送手機短信功能

    python twilio模塊實現(xiàn)發(fā)送手機短信功能

    這篇文章主要介紹了python twilio模塊實現(xiàn)發(fā)送手機短信的功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 淺析python多線程中的鎖

    淺析python多線程中的鎖

    這篇文章主要介紹了淺析python多線程中的鎖,鎖由Python的threading模塊提供,并且它最多被一個線程所持有,當(dāng)一個線程試圖獲取一個已經(jīng)鎖在資源上的鎖時,該線程通常會暫停運行,直到這個鎖被釋放,需要的朋友可以參考下
    2023-07-07

最新評論