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

Python unittest 自動(dòng)識(shí)別并執(zhí)行測(cè)試用例方式

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

自動(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)行加載。

解釋?zhuān)?/strong>

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

pattern:用例匹配原則

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

例子:

#coding=utf-8
import unittest
 
#定義測(cè)試用例的目錄為當(dāng)前目錄
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
 
if __name__ == '__main__':
 runner = unittest.TextTestRunner()
 runner.run(discover)

注釋?zhuān)?/strong>

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 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')
 #讀取測(cè)試報(bào)告正文
 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 接口自動(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 查看Django和flask版本的方法

    查看Django和flask版本的方法

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

    python中opencv?Canny邊緣檢測(cè)

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

    python中pip的安裝與使用教程

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

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

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

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

    這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹(shù)定義與使用方法,結(jié)合具體實(shí)例形式分析了Python哈夫曼樹(shù)的原理、定義及簡(jiǎn)單使用方法,需要的朋友可以參考下
    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版的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python twilio模塊實(shí)現(xiàn)發(fā)送手機(jī)短信功能

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

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

    淺析python多線程中的鎖

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

最新評(píng)論