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)行加載。
解釋?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)文章
舉例詳解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ù)定義與使用方法,結(jié)合具體實(shí)例形式分析了Python哈夫曼樹(shù)的原理、定義及簡(jiǎn)單使用方法,需要的朋友可以參考下2018-04-04
python twilio模塊實(shí)現(xiàn)發(fā)送手機(jī)短信功能
這篇文章主要介紹了python twilio模塊實(shí)現(xiàn)發(fā)送手機(jī)短信的功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08

