pytest實(shí)際上是python自帶測(cè)試框架unittest的擴(kuò)展挽荠,那么pytest是如何實(shí)現(xiàn)unittest中的setup和teardown的呢?
pytest初始化的類(lèi)別和作用域
- 模塊級(jí)別(Module level setup/teardown):作用于一個(gè)模塊內(nèi)的所有class和def类缤,對(duì)于所有class和def,setup和teardown只執(zhí)行一次
def setup_module(module):
""" setup any state specific to the execution of the given module."""
def teardown_module(module):
""" teardown any state that was previously setup with a setup_module
method.
"""
- 類(lèi)級(jí)別(Class level setup/teardown):作用于一個(gè)class內(nèi)中的所有test,所有用例只執(zhí)行一次setup邻吭,當(dāng)所有用例執(zhí)行完成后餐弱,才會(huì)執(zhí)行teardown
@classmethod
def setup_class(cls):
""" setup any state specific to the execution of the given class (which
usually contains tests).
"""
@classmethod
def teardown_class(cls):
""" teardown any state that was previously setup with a call to
setup_class.
"""
- 方法和函數(shù)級(jí)別(Method and function level setup/teardown):作用于單個(gè)測(cè)試用例,若用例沒(méi)有執(zhí)行(如被skip了)或失敗了囱晴,則不會(huì)執(zhí)行teardown
def setup_method(self, method):
""" setup any state tied to the execution of the given method in a
class. setup_method is invoked for every test method of a class.
"""
def teardown_method(self, method):
""" teardown any state that was previously setup with a setup_method
call.
"""
若用例直接寫(xiě)在模塊中膏蚓,而不是在類(lèi)中,則用:
def setup_function(function):
""" setup any state tied to the execution of the given function.
Invoked for every test function in the module.
"""
def teardown_function(function):
""" teardown any state that was previously setup with a setup_function
call.
"""
- pytest.fixture()裝飾函數(shù)畸写,結(jié)合yield實(shí)現(xiàn)初始化和teardown
舉個(gè)例子(pytest)文檔中的:
import smtplib
import pytest
@pytest.fixture(scope="module")
def smtp():
smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
yield smtp # provide the fixture value
print("teardown smtp")
smtp.close()
運(yùn)行結(jié)果:
$ pytest -s -q --tb=no
FFteardown smtp
2 failed in 0.12 seconds
pytest用例初始化操作的示例
為了體現(xiàn)初始化和環(huán)境恢復(fù)驮瞧,本節(jié)演示采用郵件發(fā)送的腳本,可查看郵件發(fā)送的腳本:python發(fā)送郵件腳本或者參考文章:SMTP發(fā)送郵件枯芬。
1论笔、setup_method(self, method)
- 在test_method.py中構(gòu)建了3個(gè)測(cè)試用例,每個(gè)用例在執(zhí)行前后都會(huì)執(zhí)行setup_method/teardown_method連接smtp和斷開(kāi)smtp千所。
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import pytest
class TestSmtp():
# 發(fā)件人和收件人,換成你自己的發(fā)件人狂魔、收件人qq號(hào)
sender = "sender@qq.com"
receivers = "rece@qq.com"
# 郵箱服務(wù)器
smtpserver = "smtp.qq.com"
smtpport = 465
# 連接郵箱服務(wù)器,qq郵箱和密碼,換成自己的
username = "sendr@qq.com"
password = "qq mail's password"
smtp = smtplib.SMTP_SSL()
def setup_method(self, method):
self.smtp.connect(self.smtpserver, self.smtpport)
self.smtp.login(self.username, self.password)
print("成功登錄")
def teardown_method(self, method):
self.smtp.quit()
print("斷開(kāi)連接")
def test_send_text(self):
# 郵件發(fā)送淫痰、接收人員最楷,郵件標(biāo)題、正文
msg = MIMEText("微信公眾號(hào)號(hào):開(kāi)源優(yōu)測(cè)", "plain", "utf-8")
msg["From"] = self.sender
msg["To"] = self.receivers
msg["Subject"] = Header("開(kāi)源優(yōu)測(cè)_DeepTest_from_chenlele_text", "utf-8")
# 發(fā)送郵件
self.smtp.sendmail(self.sender, self.receivers, msg.as_string())
def test_send_html(self):
msg = MIMEText("<p>微信公眾號(hào)號(hào):開(kāi)源優(yōu)測(cè)</p><a ,
"html",
"utf-8")
msg["From"] = self.sender
msg["To"] = self.receivers
msg["Subject"] = Header("開(kāi)源優(yōu)測(cè)_DeepTest_from_chenlele_html", "utf-8")
# 發(fā)送郵件
self.smtp.sendmail(self.sender, self.receivers, msg.as_string())
def test_send_attchment(self):
# 郵件格式說(shuō)明、發(fā)送管嬉、接收人員信息、郵件標(biāo)題
msg = MIMEMultipart()
msg["From"] = self.sender
msg["To"] = self.receivers
msg["Subject"] = Header("開(kāi)源優(yōu)測(cè)_DeepTest_from_chenlele", "utf-8")
# 構(gòu)建帶附件的郵件正文
msg.attach(MIMEText("微信公眾號(hào):開(kāi)源優(yōu)測(cè)", "plain", "utf-8"))
# 構(gòu)造附件,多個(gè)附件同理
attach1 = MIMEText(open("judge_leap.json", 'rb').read(), "base64", "utf-8")
attach1["Content-Type"] = "application/octet-stream"
# 這里filename隨意寫(xiě)朗鸠,將會(huì)在郵件中顯示
attach1["Content-Disposition"] = "attrachment;filename=code.py"
# 關(guān)聯(lián)附件到正文
msg.attach(attach1)
# 發(fā)送郵件
self.smtp.sendmail(self.sender, self.receivers, msg.as_string())
-
查看結(jié)果蚯撩,采用pytest -s -q 運(yùn)行,-s 可以查看打印信息烛占,-q減少輸出信息:
2胎挎、setup_class(cls)
- 作用于class的setup_class/teardown_class,類(lèi)中所有的用例只會(huì)執(zhí)行一次忆家,如圖所示犹菇;
- ps:用例與test_method.py的一致,參考上一串代碼芽卿。
3揭芍、setup_module(module)
- setup_module/teardown_module在一個(gè)模塊內(nèi),只會(huì)執(zhí)行一次卸例,作用于模塊內(nèi)的所有用例
- 示例中構(gòu)建了2個(gè)class和1個(gè)def称杨,共4個(gè)用例,可以看到筷转,4個(gè)用例只執(zhí)行了一次module
test_module.py
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import pytest
info = {"sender": "your@qq.com",
"receivers": "yourother@qq.com",
"smtpserver": "smtp.qq.com",
"smtpport": 465,
"username":"your@qq.com",
"password": "yourpassword",
"smtp": smtplib.SMTP_SSL()}
def setup_module(module):
info["smtp"].connect(info["smtpserver"], info["smtpport"])
info["smtp"].login(info["username"], info["password"])
print("成功登錄")
def teardown_module(module):
info["smtp"].quit()
print("斷開(kāi)連接")
class TestSendText():
def test_send_text(self):
# 郵件發(fā)送姑原、接收人員,郵件標(biāo)題呜舒、正文
msg = MIMEText("微信公眾號(hào)號(hào):開(kāi)源優(yōu)測(cè)", "plain", "utf-8")
msg["From"] =info["sender"]
msg["To"] = info["receivers"]
msg["Subject"] = Header("開(kāi)源優(yōu)測(cè)_DeepTest_from_chenlele_text", "utf-8")
# 發(fā)送郵件
info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())
def test_send_html(self):
msg = MIMEText("<p>微信公眾號(hào)號(hào):開(kāi)源優(yōu)測(cè)</p><a ,
"html",
"utf-8")
msg["From"] = info["sender"]
msg["To"] = info["receivers"]
msg["Subject"] = Header("開(kāi)源優(yōu)測(cè)_DeepTest_from_chenlele_html", "utf-8")
# 發(fā)送郵件
info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())
class TestSendAttach():
def test_send_attchment(self):
# 郵件格式說(shuō)明锭汛、發(fā)送、接收人員信息、郵件標(biāo)題
msg = MIMEMultipart()
msg["From"] = info["sender"]
msg["To"] = info["receivers"]
msg["Subject"] = Header("開(kāi)源優(yōu)測(cè)_DeepTest_from_chenlele", "utf-8")
# 構(gòu)建帶附件的郵件正文
msg.attach(MIMEText("微信公眾號(hào):開(kāi)源優(yōu)測(cè)", "plain", "utf-8"))
# 構(gòu)造附件,多個(gè)附件同理
attach1 = MIMEText(open("judge_leap.json", 'rb').read(), "base64", "utf-8")
attach1["Content-Type"] = "application/octet-stream"
# 這里filename隨意寫(xiě)日矫,將會(huì)在郵件中顯示
attach1["Content-Disposition"] = "attrachment;filename=code.py"
# 關(guān)聯(lián)附件到正文
msg.attach(attach1)
# 發(fā)送郵件
info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())
def test_send_text_out():
# 郵件發(fā)送奥此、接收人員,郵件標(biāo)題眨八、正文
msg = MIMEText("微信公眾號(hào)號(hào):開(kāi)源優(yōu)測(cè)", "plain", "utf-8")
msg["From"] =info["sender"]
msg["To"] = info["receivers"]
msg["Subject"] = Header("class外的用例執(zhí)行", "utf-8")
# 發(fā)送郵件
info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())
4、pytest.fixture()
- pytest.fixture采用yield實(shí)現(xiàn)setup和teardown操作左电,yield提供的參數(shù)為函數(shù)名稱(chēng)
- 與setup_module類(lèi)似廉侧,pytest.fixture可作用于一個(gè)模塊內(nèi)的所有def和class。區(qū)別在于篓足,必須將pytest.fixture()裝飾的函數(shù)作為參數(shù)傳遞給用例段誊。
- pytest.fixture()裝飾的函數(shù)必須作為參數(shù)傳遞給用例嗎?
1)栈拖、將class中的smtp_ini都刪除连舍,class中的用例執(zhí)行失敗,def用例執(zhí)行成功涩哟;
2)索赏、將class中test_send_text的smtp_ini保留盼玄,其余2個(gè)刪除,class中的用例都執(zhí)行成功潜腻?這是為什么呢埃儿?只有1個(gè)用力傳入了參數(shù),但所有用例都執(zhí)行成功了融涣。
3)童番、將class和def中的smtp_ini都刪除,用例全部執(zhí)行失敗威鹿。 - ps:用例內(nèi)容與test_module.py的一致剃斧,就不粘代碼了。
總結(jié)
4種方式的作用域:
- setup_method:僅作用于class用例集中的用例忽你,置于class內(nèi),每個(gè)用例都會(huì)調(diào)用一次
- setup_function:作用于獨(dú)立的def用例幼东,不可作用于class內(nèi)的用例
- setup_class:作用于class用例集中的用例,置于class內(nèi)科雳,只在class用例執(zhí)行的開(kāi)始執(zhí)行setup_class,結(jié)束時(shí)執(zhí)行teardown_class
- setup_module:作用于模塊內(nèi)的所有用例筋粗,置于class外,只在所以用例的開(kāi)始執(zhí)行setup_module,結(jié)束時(shí)執(zhí)行teardown_module
- pytest.fixture():作用于模塊內(nèi)的所有用例炸渡,但需要傳遞裝飾函數(shù)為參數(shù)娜亿,可置于class內(nèi)或class外